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

Aspose填充word数据 本文介绍了如何使用aspose进行word文档的生成,并提供了工具类供参考。可以在word中填充数据,生成wordpdf文档。 文章目录建立一个word模板 建立一个word模板 我是用WPS来生成的,和Word类似。 在word文档中,在菜单栏中依次点击插入->文档部件->域; 接着在弹出框中选择MergeField域,在域属性中填写域名,该域名即为变量名,填写完毕后点击确定即可; 模板生成 需要的模板 ... 阅读详情

640?wx_fmt=jpeg

在程序开发中经常需要将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`

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.1
sudo yum update
sudo yum install aspnetcore-runtime-2.1

转换程序编写

C#中使用libreoffice转换officepdf,网上有很多的代码示例,在这里还需要引入消息队列,整个程序是一个消息队列的消费者。简单说就是,用户上传了一个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、文件内容如下:

[Unit]Description=office to pdf service[Service]ExecStart=/usr/bin/dotnet /root/officetopdf/publish/Office2PDF.dll[Install]WantedBy=default.target
Description=office to pdf service

[Service]

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

[Install]

WantedBy=default.target

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

systemctrl daemon-reloadsystemctrl start officetopdf
systemctrl start officetopdf

示例

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


Aspose.Words for Java 体验 公司中要做一些导出word的工作,经别人推荐,使用了Aspose.Words for Java ,感觉很好用,美中不足的地方就是,它是收费软件。 原理吗?比较常规,模板+入参==》aspose引擎==》生成文档。 在里,给大家提供一个简单的DEMO: 1、Maven依赖: com.aspose aspose-words 14.9.0 jdk16 阅读详情

相关推荐

excelpdf工具类 使用aspose.cells

/ 设置页脚与页面底部的距离。* @param excelFilePath excel文件路径。* @param excelFilePath excel文件路径。* @param pdfFilePath pdf文件路径。* @param pdfFilePath pdf文件路径。* @param excelPath excel文件。// 将Excel中的图片换为PDF。// 添加Excel中的图片到PDF

m0_65643863的博客 1978

Aspose常用三套 完全破解版

Aspose 常用word excel PPT 完全破解版,支持.net4.0程序开发,无水印

Aspose实现word图片、pdf

有了Aspose.BarCode,开发者能对条形码图像的每一方面进行全面的控制:背景颜色,条形颜色,图像质量,旋角度,X尺寸,标题,客户自定义分辨率等。Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,可以有计划地操纵一些商业中最流行的文件格式:Word, Excel, PowerPoint, Project,等office文档以及PDF文档。它提供了一个简单的类集用于控制字符识别。

qq_36489998的博客 2060

Aspose破解版

Aspose破解版 包含 Aspose.Slides.dll Aspose.Words.dll Aspose.Cells.dll

java word doc docx 等office文档 为pdf,无需破解 aspose ,无水印

需求很明确: maven 下载jar, 本地执行 word 换为 PDF ,质量无损耗,免费无水印。java 代码实现。就这么点需求,找方法 还真不容易。

stomfeng的专栏 5975

谁说多功能和低价格不能兼得?Aspose系列产品1024购买指南请查收!

你还在为了Word、Excel、PDF、CAD等文档格式换而发愁吗? 你是否在寻找一款能够在应用程序中文档管理的工具呢? Aspose——支持100多种文件格式创建、编辑、换和打印! 往下看,找一找哪款产品满足您的开发需求~ ▼▼▼▼▼ ★Aspose.PDF 支持的文件格式 文本 包括文本的提取、搜索、替换、添加等功能。 图片 包括图片的添加、替换、删除等功能以及PDF文...

mnrssj的博客 2357

aspose pdf表格大小乱了_OfficePDFAspose太贵,怎么办?

在程序开发中经常需要将Office文件换成PDF,著名的Aspose的三大组件可以很容易完成这个功能,但是Aspose的每个组件都单独收费,而且每个都卖的不便宜。在老大的提示下,换了一种思路来解决这个问题。环境dotNetCore:2.1CentOS:7.5Docker:18.06.1-ce步骤1、Docker中安装libreoffice和dotNetCore;2、编写换程序;3、程...

weixin_39594296的博客 202

microsoft office root目录_OfficePDFAspose太贵,怎么办?

在程序开发中经常需要将Office文件换成PDF,著名的Aspose的三大组件可以很容易完成这个功能,但是Aspose的每个组件都单独收费,而且每个都卖的不便宜。在老大的提示下,换了一种思路来解决这个问题。环境dotNetCore:2.1CentOS:7.5Docker:18.06.1-ce步骤1、Docker中安装libreoffice和dotNetCore;2、编写换程序;3、程...

weixin_39629467的博客 606

PDF技术(一)-Java实现Office系列文件PDF文件

最近,公司要求做个文件pdf的调研报告,于是在网上找了一些实现方法,现在将这些方法做个对比,并记录下来,以后或许有用呢,哈哈。 首先说一下需求,产品要求不能使用第三方软件实现,因为这种实现方式效率不高,所以需要使用“纯Java代码”实现。同时也对跨平台有要求,系统需要运行在linux系统上。综合现阶段发现的方案,决定采用基于Aspose的方式进行实现。 好了,现在先看一下对比的结果: 各实...

晋文子上的博客 1万+

Aspose18.7

Aspose18.7,授权,其他软件上提取出来的,很好用,可以尝试

Aspose所有破解免费dll文件

Aspose所有破解免费dll文件 包含Aspose.Cells、Aspose.Diagram、Aspose.Words、Aspose.Pdf等全部dll

Java实现在线预览附件 officePDF

Java实现在线预览附件 officePDF因为项目是做OA这一块,有很多附件需要实现在线预览附件,在网上也看了很多相关的资料。主要实现方式就是 (openoffice+swftools+flexpaper)和(aspose+pdfjs预览)。主要步骤: 1.需要先将文档换为PDF文件。 2.用pdfjs预览PDF文件换步骤: * 使用OpenOffice/Aspose 将ppt、w

zhuMin的博客 1万+

wordpdf方案

java中,五种wordpdf方案对比

qq_45778701的博客 2018

java使用aspose officePDF工具类

Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、换和打印文档。官方文档:https://www.aspose.com 自己写的工具类分享记录一下 package com.feng.util; import com.aspose.cells.*; imp

z172989496的专栏 1177

Aspose.Words v18.7 C示例源码:WordPDF(无需安装Office

Aspose.Words v18.7 C#示例源码:WordPDF(无需安装Office) 【下载地址】Aspose.Wordsv18.7C示例源码WordPDF无需安装Office 本仓库提供了一个使用Aspose.Words v18.7将Word文档换为PDF文档的C#示例源码。Aspose.Words是一个强...

gitblog_09715的博客 899

asposepdf横版_aspose实现OfficePdf

aspose实现OfficePdf关键代码:jar包:aspose-words-14.6.0.jaraspose-cells-10.8.jaraspose.slides-14.4.0.jaraspose-diagram-2.1.0.jarprotectedvoidrealTransform(InputStreamin,OutputStreamout)throwsIOExceptio...

weixin_32349093的博客 175

基于java技术实现wordpdf

在现代工作环境中,文档的处理和共享是不可或缺的任务。而将Word文件换为PDF格式,已经成为了许多工作场景中的常见需求。无论是在商务合同、报告、简历还是其他文档类型中,将其换为PDF格式都具有诸多优势。首先,让我们了解一下为什么在工作中将Word文件换为PDF格式如此重要。PDF格式具有跨平台、可靠性高、内容保护性强等诸多优势。通过将文档换为PDF格式,您可以确保文档在不同操作系统和设备上的一致性显示,同时保护文档内容不被随意修改,加密等一系列操作。

qq_44033725的博客 1万+

Java-基于Aspose方式将office文件换成pdf格式

文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码如下(示例): import numpy as np import

Mr南瓜头的博客 3108
上一篇: 树莓派也跑Docker和.NET Core
下一篇: 从壹开始 [ Ids4实战 ] 之三║ 详解授权持久化 & 用户数据迁移
dotNET跨平台
博客等级 码龄9年 6206粉丝 1791原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值