方案1. OpenOffice + jodconverter
1)介绍
OpenOffice 是一套跨平台的办公室软件服务,支持Word转PDF,能在Windows、Linux、MacOS X (X11)和 Solaris 等操作系统上安装及运行。OpenOffice是自由软件,任何人都可以免费下载、使用及推广它。
JodConverter是一款利用OpenOffice进行转化的工具,可以在Office文件和OpenOffice文件之间进行转换,同时也可以将Office或者OpenOffice系列文件转化位PDF,最新版本是2.2.2。
2) 实现思路
通过IP+端口号调用远程的OpenOffice服务实现转换
3)实现demo
一、安装OpenOffice服务
二、在项目中添加POM依赖
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version> 2.2 . 1 </version> </dependency> |
三、使用demo
public static void test() { String connectIp = "10.0.0.1" ; int port = 8100 ; String docPath = "/home/pdf/v3.doc" ; String pdfPath = "/home/pdf/v4.pdf" ; docToPdf(docPath , pdfPath, connectIp, port); } /** * doc转pdf * * @param inputPath * @param outputPath * @throws IOException */ public static void docToPdf(String inputPath, String outputPath, String connectIp, int port) { File inputFile = new File(inputPath); File outputFile = new File(outputPath); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, port); try { connection.connect(); } catch (ConnectException e) { // TODO Auto-generated catch block e.printStackTrace(); } // convert DocumentConverter converter = getConverter(connectIp, connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect(); } /** * 获取转换器 * * @param connectIp * @param connection * @return */ private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) { DocumentConverter converter = "localhost" .equals(connectIp) || "127.0.0.1" .equals(connectIp) || "0:0:0:0:0:0:0:1" .equals(connectIp) ? new OpenOfficeDocumentConverter(connection) : new StreamOpenOfficeDocumentConverter(connection); return converter; } |
4)优劣势
优点:转换速度快,单文档平均转换速度300毫秒;转换效果较好,很少发生错位。
缺点:需要安装OpenOffice服务,比较麻烦;单线程,高并发时程序挂起。
方案2. Aspose.Words
1)介绍
Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、转换和打印文档。
2)实现思路
通过调用类库中的方法实现转换
3)实现demo
一、将license.xml放在src/main/resource下
license.xml中内容如下:
Expand source
<License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry> 20991231 </SubscriptionExpiry> <LicenseExpiry> 20991231 </LicenseExpiry> <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber> </Data> <Signature> sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= </Signature> </License> |
二、添加POM依赖(因为是第三方商业类库,我提前把jar包deploy到了我们的私服上了)
<dependency> <groupId>aspose</groupId> <artifactId>words</artifactId> <version> 1.0 . 0 </version> </dependency> |
三、使用demo
/** * doc转pdf * * @param inputPath * @param outputPath * @throws ConnectException */ public static void docToPdf(String inputPath, String outputPath) throws Exception { if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 return ; } File file = new File(outputPath); FileOutputStream os = new FileOutputStream(file); Document doc = new Document(inputPath); doc.save(os, SaveFormat.PDF); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, } private static boolean getLicense() throws Exception { boolean result = false ; InputStream is = FileUtils. class .getClassLoader().getResourceAsStream( "license.xml" ); License aposeLic = new License(); aposeLic.setLicense(is); result = true ; return result; } |
4)优劣势
优点:无需安装服务,JAR包依赖,方便使用; 转换效果好,不失真,不错位;转换速度快,单文档平均转换速度200毫秒。
缺点:商业类库;首次转换比较耗时,大概需2~4秒。