市场上主流的 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);

}

}

doc 2 pdf java_java doc转pdf相关推荐

  1. R语言使用compareGroups包compareGroups函数生成表统计表、createTable函数创建二元表、并导出结果到文档(doc、csv、xlsx、pdf)

    R语言使用compareGroups包compareGroups函数生成表统计表.createTable函数创建二元表.并导出结果到文档(doc.csv.xlsx.pdf) 目录 R语言使用compa ...

  2. PDF与doc格式互换

    PDF与doc格式互换 在当今的计算机世界里,使用率最高的两种文档方式分别是Microsoft Word的Doc格式和Adobe Acrobat的Pdf格式文件.由于微软的***,我们现在所使用的绝大 ...

  3. 【script】python实现多线程将doc、docx转pdf,doc转docx

    摘要 本文讲述通过python实现多线程将当前目录下的 .doc文档转.pdf..docx文档转.pdf,以及.doc文档转.docx(在python中,如需对word文档进行读写,只能读写docx格 ...

  4. Jquery导出页面表格table的内容为Excel,PDF,DOC格式

    Jquery导出页面表格table的内容为Excel,PDF,DOC格式 文档地址:http://www.jqueryfuns.com/resource/2381 1.引入jquery的js–这里就不 ...

  5. Flash在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等

    先看一下效果   特点: 可以阅读.打印.进行格式转换. 可以打开本地或网络文件. 可识别的格式:pdf.doc.docx.xls.xlsx.ppt.pptx.htm.txt.rtf.epub.csv ...

  6. Flex在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等

    先看一下效果   特点: 可以阅读.打印.进行格式转换. 可以打开本地或网络文件. 可识别的格式:pdf.doc.docx.xls.xlsx.ppt.pptx.htm.txt.rtf.epub.csv ...

  7. 【githubshare】开源的文件文档在线预览项目,支持主流办公文档的在线预览,如 doc、docx、Excel、pdf、txt、zip、rar、 图片等

    GitHub 上一份硬核计算机科学 CS 自学计划,偏向软件工程和系统架构方向. 旨在帮助开发者制定一个为期 3-5 年的重学 CS 目标,夯实 CS 基本功,达到美国一流大学 CS 专业本科毕业水平 ...

  8. 轻松实现各种文档格式转换,doc转pdf、doc转png图片、pdf转png图片,可以实现Windows、Linux、MacOS平台上部署(附源码和说明)

    轻松实现各种文档格式转换,doc转pdf.doc转png图片.pdf转png图片,可以实现Windows.Linux.MacOS平台上部署,可以通过命令行.shell脚本.python等方式调用. 实 ...

  9. java doc转换docx_java PDF转DOC.docx

    java PDF转DOC 实现adobe PDF 转 office wordJNI Java Native InterfaceJAVA本地调用首先,如果我们要用JACOB至少需要对JNI有些基本的了解 ...

最新文章

  1. (七)渐变 矩形渐变 放射渐变
  2. artDIalog 弹出层
  3. Irrlicht例002--Quake3Map
  4. Python设置显示屏分辨率
  5. 如何启动Django项目详解
  6. 软件科技创新创业基地暑期集训有感
  7. 我的日程安排表 II
  8. 电信IHO-3000机顶盒高安版 悦ME刷机固件
  9. TestCenter测试管理工具功能详解十(O)
  10. 亚马逊云技术防范勒索病毒
  11. PGSQL 模糊查询不区分大小写
  12. APM,云时代的应用性能优化神器
  13. 微信小程序-创建自己的小程序帐号
  14. 大一新生必看,自学必看,里昂详解数据结构之二叉树
  15. Ubuntu Desktop 安装谷歌拼音输入法
  16. 录制iPhone屏幕
  17. 多激光雷达外参标定算法与源码解析(一):基于BLAM的建图模块
  18. Scratch3.0——助力新进程序员理解程序(一、基础使用与运动)
  19. 数学证明到底是什么?
  20. Java运算符-逻辑运算符

热门文章

  1. html css图片炫酷效果,5种CSS3 Transitions炫酷图片标题特效
  2. ts453bmini 内存_为华硕天选游戏本而生:8GB DDR4-3200单条跌至194,补齐内存短板
  3. 计算机与音乐结合的论文题目,最新音乐本科毕业论文题目
  4. 考研英语如何翻译一个句子
  5. 基于ssm和mysql的智能停车系统、javaweb实现bs结构的停车场智能车位出入管理
  6. 2022金属非金属矿山(露天矿山)主要负责人考试模拟100题及在线模拟考试
  7. 安卓炫酷的抽屉菜单——JPSpringMenu
  8. RSTP(端口角色+端口状态+工作机制)|||| 交换机接口分析
  9. TiDB 中的高可用实践
  10. IP定位领域中相关名词解释