Office转PDF,Aspose太贵,怎么办?
在程序開發中經常需要將Office文件轉換成PDF,著名的Aspose的三大組件可以很容易完成這個功能,但是Aspose的每個組件都單獨收費,而且每個都賣的不便宜。在老大的提示下,換了一種思路來解決這個問題。
環境
dotNetCore:2.1
CentOS:7.5
Docker:18.06.1-ce
步驟
1、Docker中安裝libreoffice和dotNetCore;
2、編寫轉換程序;
3、程序以服務的方式部署在Docker中。
配置Docker環境
因為需要部署dotNetCore的程序,開始的想法是依賴microsoft/dotnet:2.1-aspnetcore-runtime鏡像創建容器,然后在容器中安裝libreoffice,后來發現容器中沒法執行yum命令(可能是沒找到方法)。最后換了一種思路,依賴centos鏡像創建容器,在容器中安裝dotNetCore2.1和libreoffice。
安裝`libreofiicie`
yum?install?libreoffice?安裝`dotnetCore2.1`
sudo?rpm?-Uvh?https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpmsudo?yum?updatesudo?yum?install?aspnetcore-runtime-2.1sudo?yum?update
sudo?yum?install?aspnetcore-runtime-2.1
轉換程序編寫
在C#中使用libreoffice轉換office為pdf,網上有很多的代碼示例,在這里還需要引入消息隊列,整個程序是一個消息隊列的消費者。簡單說就是,用戶上傳了一個office文件,上傳成功后會發一個消息,該程序中接收到消息就進行轉換。
消息監聽
????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();????????????}????????}????}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();
????????????}
????????}
????}
文件轉換
????????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;????????}????????????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、文件內容如下:
Description=office?to?pdf?service
[Service]
ExecStart=/usr/bin/dotnet?/root/officetopdf/publish/Office2PDF.dll
[Install]
WantedBy=default.target
4、使用下面命令創建和啟動服務;
systemctrl?daemon-reloadsystemctrl?start?officetopdfsystemctrl?start?officetopdf
示例
https://github.com/oec2003/StudySamples/tree/master/Office2PDF
總結
以上是生活随笔為你收集整理的Office转PDF,Aspose太贵,怎么办?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 ASP.NET Core 中安装 M
- 下一篇: 从壹开始 [ Ids4实战 ] 之三║