市场上主流的 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下:

  1. tar -xvzf Apache_OpenOffice_4.1.3_Linux_x86-64_install-deb_zh-CN.tar.gz
  2. cd zh-CN/DEBS/
  3. sudo dpkg -i *.deb
  4. cd desktop-integration/
  5. 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端口的服务)

package openofficeTest;  import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;  import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  public class Word2Pdf {  public static int PORT = 8100;  public static void main(String[] args){  String path1 = "/tmp/1.doc";  String path2 = "/tmp/2.pdf";  try {  wordToPdf(path1, path2);  } catch (Exception e) {  e.printStackTrace();  }  }  public static void wordToPdf(String path1, String path2)  throws Exception {  File file1 = new File(path1);  File file2 = new File(path2);  // 获得文件格式  DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();  DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");  DocumentFormat docFormat = formatReg.getFormatByFileExtension("doc");  // stream 流的形式  InputStream inputStream = new FileInputStream(file1);  OutputStream outputStream = new FileOutputStream(file2);  /** *  */  OpenOfficeConnection connection = new SocketOpenOfficeConnection(PORT);  System.out.println(connection);  try {  connection.connect();  DocumentConverter converter = new OpenOfficeDocumentConverter(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) throws Exception{File tempDir = null;try{tempDir = Files.createTempDir();String canonicalPath = tempDir.getCanonicalPath();File file = new File(canonicalPath + "/" + sourceFileName);OutputStream os = new FileOutputStream(file);BufferedOutputStream bufferedOutput = new BufferedOutputStream(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<String> output = new ArrayList<String>();BufferedReader br = new BufferedReader(new InputStreamReader(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(new FileInputStream(file2));}}return null;}finally{if(tempDir != null)FileUtils.deleteDirectory(tempDir);}}

转载于:https://www.cnblogs.com/yxy-ngu/p/7100642.html

【java】doc转pdf相关推荐

  1. doc转pdf java不失真,java doc转PDF

    有时候需要把doc文件转成PDF文件,比如需要在网页上预览doc文件.那么可以使用xdocreport这个库. xdocreport是基于itext的,使用非常简单只需要: XWPFDocument ...

  2. Java DOC 转换给 PDF 格式文档的代码

    工作过程,把写代码过程经常用的代码片段备份一次,下面的代码段是关于Java DOC 转换给 PDF 格式文档的代码,应该对码农们有所用. import java.io.File; import jav ...

  3. java使用freemark实现word(.doc/.docx)/pdf生成和导出(附源码和模板文件)

    freemark生成word/pdf 一. 背景 二.实现的技术选型以及遇到的坑 三.最终的效果 2.1 `.doc` word效果展示 2.1 `.docx` word效果展示 2.2 docx w ...

  4. java文件预览_java 在线预览doc,pdf

    先说一说如何实现在线预览doc网上查了很多资料,基本思路就是将doc 转为 pdf,由于低版本浏览器不支持预览pdf,所以基本是再将pdf 转为 swf. 由于我这次做的需求只需要兼容chrome即可 ...

  5. 麒麟系统java调用wps转pdf,急java调用WPS或pdfcreator的com接口实现doc转pdf解决方法

    急急急java调用WPS或pdfcreator的com接口实现doc转pdf 各位大虾:我想把word文件生成PDF,然后网上找了方法http://hacker507.iteye.com/blog/1 ...

  6. java doc to pdf_java代码doc转pdf提高效率的方法

    publicclassDocTransePdf{staticfinalintwdDoNotSaveChanges=0;//不保存待定的更改.staticfinalintwdFormatPDF=17;/ ...

  7. java 文件在线预览_java 在线预览doc,pdf

    先说一说如何实现在线预览doc 网上查了很多资料,基本思路就是将 doc 转为 pdf,由于低版本浏览器不支持预览 pdf,所以基本是再将 pdf 转为 swf (使用FlexPaper + swft ...

  8. java word转pdf linux_Linux平台中使用PHP把word转pdf的实现方法

    Linux平台中使用PHP把word转pdf的实现方法 1.ubantu下安装libreoffice sudo apt-get install libreoffice 2.命令行执行word转pdf ...

  9. java word转pdf 后通过 PdfReader 和 PdfStamper对pdf添加水印 通过poi等组件实现

    所需jar包地址 <!-- java 读取word文件里面的加颜色的字体 转pdf 使用 --><dependency><groupId> e-iceblue &l ...

  10. java实现word,pdf,excel,图片添加水印

    gitee项目地址:https://gitee.com/betelnutandwine/meutilswatermark: java实现pdf,word,excel,ppt,图片加水印 jar地址:s ...

最新文章

  1. Google App Engine(GAE)入门教程翻译
  2. 大叔公开课~微服务与持久集成
  3. 进程的逻辑设备如何与一个物理设备建立对应的关系?
  4. java list作为参数传递_Java 程序将lambda表达式作为方法参数传递
  5. 智慧中国杯算法赛解读 | 精准资助数据探索(一)
  6. jquery 事件冒泡的介绍以及如何阻止事件冒泡
  7. RabbitMQ实现(并发)多线程处理消息
  8. 年薪50万的程序员_985程序员年薪50万,看似风光,但当事人却想转行
  9. 谷歌紧急修复已遭利用的新 0day
  10. 树莓派上搭建ActiveMQ
  11. 【渝粤教育】国家开放大学2018年春季 0104-21T酒店安全管理 参考试题
  12. spark aggregate函数详解
  13. latex安装教程以及入门
  14. 三个元素的矩阵乘除法
  15. 图书管理系统设计总结与心得
  16. NLP领域表达退化各向异性理解及对应策略总结
  17. html app签名,html5手写签名
  18. 查看linux最大的sftp连接数,Linux查看某个端口的连接数的方法
  19. Word2010设置显示中文几号字
  20. 输出信噪比公式_如何计算信号的信噪比

热门文章

  1. iframe 滚动条不显示_BUG赏金 | 当我发现iFrame注入时的利用
  2. java语言是那年_Java语言是在()年正式推出的_学小易找答案
  3. clickhouse 数据存储
  4. kafka 心跳参数
  5. 一文讲清模拟信号、自然信号、数字信号、模拟输入输出
  6. Java_String_01_由转义字符串得到其原本字符串
  7. Linux用户、群组及权限
  8. C语言 AI智能,五子棋 人机对战,人人对战
  9. linux下安装php扩展curl
  10. [转]Flex 中的皮肤