前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Office转PDF,Aspose太贵,怎么办?

Office转PDF,Aspose太贵,怎么办?

作者头像
oec2003
发布2019-07-19 17:59:29
5.8K0
发布2019-07-19 17:59:29
举报
文章被收录于专栏:不止dotNET

在程序开发中经常需要将Office文件转换成PDF,著名的Aspose的三大组件可以很容易完成这个功能,但是Aspose的每个组件都单独收费,而且每个都卖的不便宜。在老大的提示下,换了一种思路来解决这个问题。

环境

dotNetCore:2.1 CentOS:7.5 Docker:18.06.1-ce

步骤

1、Docker中安装libreofficedotNetCore; 2、编写转换程序; 3、程序以服务的方式部署在Docker中。

配置Docker环境

因为需要部署dotNetCore的程序,开始的想法是依赖microsoft/dotnet:2.1-aspnetcore-runtime镜像创建容器,然后在容器中安装libreoffice,后来发现容器中没法执行yum命令(可能是没找到方法)。最后换了一种思路,依赖centos镜像创建容器,在容器中安装dotNetCore2.1libreoffice

安装`libreofiicie`

代码语言:javascript
复制
yum install libreoffice 

安装`dotnetCore2.1`

代码语言:javascript
复制
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install aspnetcore-runtime-2.1

转换程序编写

C#中使用libreoffice转换officepdf,网上有很多的代码示例,在这里还需要引入消息队列,整个程序是一个消息队列的消费者。简单说就是,用户上传了一个office文件,上传成功后会发一个消息,该程序中接收到消息就进行转换。

消息监听

代码语言:javascript
复制
    class Program
    {
        static IPowerPointConverter converter = new PowerPointConverter();

        static void Main(string[] args)
        {

            var mqManager = new MQManager(new MqConfig
            {
                AutomaticRecoveryEnabled = true,
                HeartBeat = 60,
                NetworkRecoveryInterval = new TimeSpan(60),

                Host = ConfigurationManager.AppSettings["mqhostname"], 
                UserName = ConfigurationManager.AppSettings["mqusername"],
                Password = ConfigurationManager.AppSettings["mqpassword"],
                Port = ConfigurationManager.AppSettings["mqport"]
            });


            if (mqManager != null && mqManager.Connected)
            {
                Console.WriteLine("RabbitMQ连接初始化成功。");
                Console.WriteLine("RabbitMQ消息接收中...");

                mqManager.Subscribe<PowerPointConvertMessage>(message =>
                {
                    if (message != null)
                    {
                        converter.OnWork(message);
                        Console.WriteLine(message.FileInfo);
                    }
                });
            }
            else
            {
                Console.WriteLine("RabbitMQ连接初始化失败,请检查连接。");
                Console.ReadLine();
            }
        }
    }

文件转换

代码语言:javascript
复制
        public bool OnWork(MQ.Messages Message)
        {
            PowerPointConvertMessage message = (PowerPointConvertMessage)Message;
            string sourcePath = string.Empty;
            string destPath = string.Empty;
            try
            {
                if(message == null)
                    return false;
                Stream sourceStream = fileOperation.GetFile(message.FileInfo.FileId);
                string filename = message.FileInfo.FileId;
                string extension = System.IO.Path.GetExtension(message.FileInfo.FileName);
                sourcePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), filename + extension);
                destPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), string.Format("{0}.pdf", filename));

                if (!SaveToFile(sourceStream, sourcePath))
                    return false;
                var psi = new ProcessStartInfo("libreoffice", string.Format("--invisible --convert-to pdf  {0}", filename + extension)) { RedirectStandardOutput = true };
                // 启动
                var proc = Process.Start(psi);
                if (proc == null)
                {
                    Console.WriteLine("不能执行.");
                    return false;
                }
                else
                {
                    Console.WriteLine("-------------开始执行--------------");
                    //开始读取
                    using (var sr = proc.StandardOutput)
                    {
                        while (!sr.EndOfStream)
                        {
                            Console.WriteLine(sr.ReadLine());
                        }
                        if (!proc.HasExited)
                        {
                            proc.Kill();
                        }
                    }
                    Console.WriteLine("---------------执行完成------------------");
                    Console.WriteLine($"退出代码 : {proc.ExitCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                if (File.Exists(destPath))
                {
                    var destFileInfo = UploadFile(destPath, string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(message.FileInfo.FileName)));
                }
                if (File.Exists(destPath))
                {
                    System.IO.File.Delete(destPath);
                }
            }
            return true;
        }

上面只是一些代码片段,完整示例会上传到Github上,文章末尾会给出地址。

部署代码到Docker

此程序是dotNetCore编写的控制台程序,希望以服务的方式在后台运行,下面介绍怎样将控制台程序以服务的方式运行:

1、将发布后的代码放在容器的/root/officetopdf/publish目录中 2、在 /lib/systemd/system目录中创建文件officetopdf.service; 3、文件内容如下:

代码语言:javascript
复制
[Unit]
Description=office to pdf service

[Service]
ExecStart=/usr/bin/dotnet /root/officetopdf/publish/Office2PDF.dll

[Install]
WantedBy=default.target

4、使用下面命令创建和启动服务;

代码语言:javascript
复制
systemctrl daemon-reload
systemctrl start officetopdf

示例

https://github.com/oec2003/StudySamples/tree/master/Office2PDF

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 不止dotNET 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境
  • 步骤
  • 配置Docker环境
    • 安装`libreofiicie`
      • 安装`dotnetCore2.1`
      • 转换程序编写
        • 消息监听
          • 文件转换
          • 部署代码到Docker
          • 示例
          相关产品与服务
          容器镜像服务
          容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档