市场上主流的 WORD 转 PDF 工具有两个:OpenOffice 和 Microsoft Office 转

换插件,可以通过部署这两个工具实现 WORD 转 PDF 功能。

1:

Microsoft 提 供 了 一 个 转 换 插 件 实 现 Office 转 PDF 功 能 , 即

SaveAsPDFandXPS。此插件是一个 com 组件,对于 C++、C#等语言可以直接使

用,如果是 JAVA 语言,需要通过 jacob 来调用 com 组件。

SaveAsPDFandXPS 插件要求必须有一台 Windows 服务器作为转换服务器安

装部署 Microsoft Office2007 以上的版本,然后再安装 SaveAsPDFandXPS 插件。

最后调用 com 组件实现转换。

官网地址:https://msdn.microsoft.com/en-us/library/dd301166(v=nav.90).aspx

2.

OpenOffice 是个开源的办公套件,提供了与 MS Word,Excel,PowerPoint 等

对应的多个软件,它支持包括 MS Office 2007 在内的多种格式,并且能够将其导

出为 PDF 文件。

这个方案是在 linux 服务器上安装 openOffice 然后通过 openOffice 命令来转换

pdf。

官方网址:http://www.openoffice.org/

在ubuntu下:

tar -xvzf Apache_OpenOffice_4.1.3_Linux_x86-64_install-deb_zh-CN.tar.gz

cd zh-CN/DEBS/

sudo dpkg -i *.deb

cd desktop-integration/

sudo dpkg -i openoffice4.1-debian-menus_4.1.3-9783_all.deb

soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &

启动服务,# netstat -an|more,可查看是否启动成功(是否有8100端口的服务)

packageopenofficeTest;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.ConnectException;importcom.artofsolving.jodconverter.DefaultDocumentFormatRegistry;importcom.artofsolving.jodconverter.DocumentConverter;importcom.artofsolving.jodconverter.DocumentFormat;importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;public classWord2Pdf {public static int PORT = 8100;public static voidmain(String[] args){

String path1= "/tmp/1.doc";

String path2= "/tmp/2.pdf";try{

wordToPdf(path1, path2);

}catch(Exception e) {

e.printStackTrace();

}

}public static voidwordToPdf(String path1, String path2)throwsException {

File file1= newFile(path1);

File file2= newFile(path2);//获得文件格式

DefaultDocumentFormatRegistry formatReg = newDefaultDocumentFormatRegistry();

DocumentFormat pdfFormat= formatReg.getFormatByFileExtension("pdf");

DocumentFormat docFormat= formatReg.getFormatByFileExtension("doc");//stream 流的形式

InputStream inputStream = newFileInputStream(file1);

OutputStream outputStream= newFileOutputStream(file2);/****/OpenOfficeConnection connection= new SocketOpenOfficeConnection(PORT);

System.out.println(connection);try{

connection.connect();

DocumentConverter converter= newOpenOfficeDocumentConverter(connection);converter.convert(inputStream, docFormat, outputStream, pdfFormat);

}catch(ConnectException e) {

e.printStackTrace();

}finally{if (connection != null) {

connection.disconnect();

connection= null;

}

}

}

}

但是,经过测试,openoffice转换的速度明显很慢,主要是在获取OpenOfficeConnection这块,我目前还没有找到能明显提升速度的方法,下面还有第三种基于libreoffice做转换的方式。

前提条件:要安装libreoffice, libreoffice-headless

安装命令:

yum install libreoffice -y

yum install libreoffice-headless -y

转换命令:libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir /tmp/ /tmp/test.doc

其中/tmp/test.doc为测试用的doc文件,生成的pdf文件会在/tmp/下,名称会默认和doc的名字一样。

下面是项目中以doc文件流输入,返回pdf文件流的方法。

public static byte[] toPDF(byte[] b, String sourceFileName) throwsException{

File tempDir= null;try{

tempDir=Files.createTempDir();

String canonicalPath=tempDir.getCanonicalPath();

File file= new File(canonicalPath + "/" +sourceFileName);

OutputStream os= newFileOutputStream(file);

BufferedOutputStream bufferedOutput= newBufferedOutputStream(os);

bufferedOutput.write(b);

String command= "libreoffice";

Process proc= new ProcessBuilder(command, "--headless", "--convert-to", "pdf:writer_pdf_Export", "--outdir", canonicalPath, canonicalPath + "/" +sourceFileName)

.redirectErrorStream(true)

.start();

ArrayList output = new ArrayList();

BufferedReader br= new BufferedReader(newInputStreamReader(proc.getInputStream()));

String line= null;while ((line = br.readLine()) != null)

output.add(line);

logger.info("执行pdf转换命令的输出:" +StringUtils.join(output, System.lineSeparator()));if (0 !=proc.waitFor())throw new Exception("转换失败");

File[] files=tempDir.listFiles();for(File file2 : files) {if (file2.getPath().endsWith(".pdf")) {return IOUtils.toByteArray(newFileInputStream(file2));

}

}return null;

}finally{if(tempDir != null)

FileUtils.deleteDirectory(tempDir);

}

}

java doc 转 pdf_java doc转pdf(示例代码)相关推荐

  1. java注解教程 pdf_Java注解教程和示例

    Java注解教程和示例 塔尼亚·辛格 Java注解使我们可以将元数据信息添加到源代码中,尽管它们不是程序本身的一部分.注解是从JDK 5添加到Java的.注解对其注解的代码的操作没有直接影响(即,它不 ...

  2. java memento_Java备忘录(Memento)模式实现的示例代码

    Java备忘录(Memento)模式实现的示例代码 /** * 数据对象 * @author stone * */ public class DataState { private String ac ...

  3. java poi导出pdf_Java POI 生成PDF文档,很给力!

    package poi.itext; import Java.io.FileOutputStream; import java.io.IOException; import java.awt.Colo ...

  4. java maven 读写pdf_Java动态生成pdf文件(使用itext编辑pdf)

    一.创建pdf模板 使用PDFelement制作pdf模板(数据域的名称对应后面插入的key) 二.导入maven依赖 com.itextpdf itextpdf 5.5.13 com.itextpd ...

  5. java微服务pdf_Java微服务pdf

    摘要 适读人群 :本书适合想要了解微服务架构,以及想要深入了解如何有效地实施企业级微服务的Java开发人员. 在本书中可以学到: ■ 使用领域驱动设计方法来设计和实现微服务 ■ 使用Spring Se ...

  6. char栈java,Java数据结构与算法-栈和队列(示例代码)

    (摘录加总结)------ 栈和队列不属于基础的数据结构,它们都属于线性表. 一.栈 对于栈存储操作元素只能在栈结构的一端进行元素的插入和删除,是一种性质上的线性表结构.按照"先进后出&qu ...

  7. java 时间api源码,时间API(示例代码)

    1. 时间API 我们的时间在java里是long类型的整数,这个整数称之为时间戳(也叫格林威治时间),即从1970-01-01到现在为止所经过的毫秒数,单有这个时间戳是不能准确表达世界各地的时间,还 ...

  8. Linkis1.0用户使用文档:JAVA和SCALA调用Linkis的接口示例代码

    Linkis 提供了方便的JAVA和SCALA调用的接口,只需要引入linkis-computation-client的模块就可以进行使用,1.0后新增支持带Label提交的方式,下面将对兼容0.X的 ...

  9. java模拟登陆_java-模拟登陆练习(示例代码)

    package com.day5.test; import java.util.Scanner; public class Test1 { /** * @param args * 需求:模拟登陆,给三 ...

  10. JAVA包装类及自动封包解包示例代码

    在学习上是一个知识点,但不知如何与实际串起来... 悲哀,真是悲哀!!! 代码: 1 public class Wrapper { 2 public static void main(String[] ...

最新文章

  1. js 闭包 实战1
  2. 使用flask-WTF,Flask-Login,sqlite3实现登录和注册(前端登录和注册页面)
  3. 比MAE更强,FAIR新方法MaskFeat用HOG刷新多个SOTA
  4. UIImageView图片视图的基本概念和使用方法
  5. 开发过程中的常见问题
  6. post install error,please remove node_moules before retry
  7. 【转】Asp.net控件开发学习笔记整理篇 - WebControl基类
  8. axure rp 8.0
  9. ygo游戏王卡组_游戏王:二线卡组脱颖而出的战将,混沌青眼卡组,仪式卡组的骄傲...
  10. word2003计算机考试题,[2018职称计算机Word2003考前练习题] 2018年职称计算机考试练习题库...
  11. yii2不用composer使用redis
  12. This time, ZTE has released the world‘s first
  13. 2020年智能物联网设备数量将达204亿,是智能为王还是服务为王?
  14. 学校年检计算机台账,学校年检整改报告.docx
  15. 从MySQL复制功能中得到一举三得实惠
  16. 拓端tecdat|R语言解决最优化问题-线性规划(LP)问题
  17. 启动docker容器时报iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport错误
  18. diskpart命令详解
  19. C++ 类(静态成员和静态函数)
  20. Delphi with Lua5.1.3

热门文章

  1. 树莓派crt安装中文字库和中文输入法
  2. html caption 靠左,HTML caption align 属性 | Paoo教程
  3. lumion计算机丢失string,Lumion计算机丢失string
  4. 省电指南-ESP8266如何降低功耗
  5. 二维码是什么?java生成二维码
  6. 6000字跟你讲清数据运营到底是做什么的?
  7. 做运营活动的几点心得
  8. 组合数有关的一些求和公式
  9. 基于LSTM的IMDB电影评论情感分析
  10. EIA/TIA 568A 568B 标准