word转pdf的最佳实现方案
来源:互联网 发布:胜通软件多少钱 编辑:程序博客网 时间:2024/06/07 23:18
最近项目里面有个需求,要求按照单位汇总所有的上报材料,最终全部导出为一本PDF格式的电子书
我查看了很多资料,最终决定使用openoffice+pdfbox两种插件的组合,去实现多个word文件转换为一个PDF文件(ppt、excel都可以转换)的功能。
开始想使用poi+itext的方案,但是看了实现的原理,决定放弃此方法,因为poi首先把word读为流写成HTML文件,在这个过程中,word所有的格式都丢失了,再去做转换的工作都已经是没有意义了。所以采用poi就是一个不好的思路。
一下说一说这种方案的实现原理:openoffice负责把word转换为pdf,pdfbox负责把多个pdf文件合并为一个。
具体实现步骤:
1 需要安装的软件:
OpenOffice 下载地址http://www.openoffice.org/
JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,将JodConverter解压缩以后,把lib下面的jar包全部添加到项目中
2 核心代码, 格式转换部分
/** * @描述: 把文件导出为PDF文件。 * @author liu.fangjun * @日期 2013-07-12 */@Controller@RequestMapping("/office2PDF")public class PageOfficeFile extends BaseController{@Value("${upload.file.path}")private String sourcepath;@Value("${upload.pdf.path}")private String targetpath;@Value("${upload.openoffice.path}")private String sofficepath;@Autowiredprivate ProjectService projectService;@Autowiredprivate SysTAttachmentService attachmentService;@Autowiredprivate SysTAttachmentDao sysTAttachmentDao;@Autowiredprivate ProjectReportService projectReportService;/** * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为 * http://www.openoffice.org/ * @param sourceFile 源文件, 可以是Office2003-2007全部格式的文档,包括.doc,.docx, .xls, .xlsx, .ppt, .pptx等. * @param destFile 目标文件. * @return 操作成功与否的提示信息: 如果返回 0, 表示找不到源文件; 如果返回-1,则表示操作失败; 返回1, 则表示转换并合并成功 * @throws Exception */ @RequestMapping("/word2pdf")@ResponseBody@Transactional public int office2PDF(String projectId) throws Exception{ //目标文件String folder = targetpath.trim();String destFile = "";//写文件之前清空文件夹,避免文件不断的追加到尾部delFolder(folder);//获取该项目下的所有监理资料文件(源文件)String sourcePath = this.getSourceFile(projectId);String sFile[] = sourcePath.split(",");//获得所属项目名称 String projectName = ""; List<Project> list = this.projectService.findByNamedParam("projectId", projectId); if(list.size()>0){ projectName = list.get(0).getProjectName(); } try { //循环读取来源文件做转换 File inputFile = null; for(int i=0;i<sFile.length;i++){ inputFile = new File(sFile[i]); if (!inputFile.exists()) { return 0;// 找不到源文件,则返回0 } destFile = targetpath.trim()+File.separator+i+".pdf"; // 如果目标路径不存在, 则新建该路径 File outputFile = new File(destFile); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // String OpenOffice_HOME = "C:\\Program Files\\OpenOffice 4";//这里是OpenOffice的安装目录 String OpenOffice_HOME = sofficepath; //Linux下OpenOffice的安装目录 // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\' if(!OpenOffice_HOME.endsWith("\\") && !OpenOffice_HOME.endsWith("/")) { OpenOffice_HOME += File.separator; } // 启动OpenOffice的服务 String command = OpenOffice_HOME+ "soffice -headless -accept=\"socket,host=localhost,port=8100;urp;\" -nofirststartwizard &";//Linux系统使用的命令 //"program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; --windows系统使用的命令 Process pro = Runtime.getRuntime().exec(command); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); connection.connect(); // 转换 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // 关闭连接 connection.disconnect(); // 关闭OpenOffice服务的进程 pro.destroy(); } //调用合并方法,生成监理资料的书本 Pages2OnePDF book = new Pages2OnePDF(); book.theBook(folder,projectName+"监理资料.pdf"); //上传pdf书本到指定下载路径,并写入附件信息表 int n = this.saveAndUpload(projectId, projectName); if(n!=1){ return -1; } } catch (Exception e) { e.printStackTrace(); return -1; } return 1; }
3 多个文件合并部分
引用pdfbox ,下载地址:http://download.csdn.net/detail/yanning1314/4852276
package com.cmcc.zysoft.szjl.pc.controller;import java.io.File;import java.io.IOException;import org.apache.pdfbox.util.PDFMergerUtility;/** * 多个pdf文件合并成一个 * 使用pdfbox插件(跨平台、不需要语言包) * @author liu.fangjun */public class Pages2OnePDF {private static String[] getFiles(String folder) throws IOException{File _folder = new File(folder);String[] filesInFolder; if(_folder.isDirectory()){filesInFolder = _folder.list();return filesInFolder;}else{throw new IOException("Path is not a directory");}}public void theBook(String folder,String bookName) throws Exception {try{PDFMergerUtility mergePdf = new PDFMergerUtility();String destinationFileName = bookName;String[] filesInFolder = getFiles(folder); for(int i = 0; i < filesInFolder.length; i++)mergePdf.addSource(folder + File.separator + filesInFolder[i]);mergePdf.setDestinationFileName(folder + File.separator + destinationFileName);mergePdf.mergeDocuments();}catch(Exception e) { e.printStackTrace();}System.out.print("success");}}
- word转pdf的最佳实现方案
- C# Word文档转PDF的实现
- C# Word文档转PDF的实现
- 能实现word转pdf的转换器
- Jacob实现word转pdf
- word转pdf实现总结
- word转pdf(java实现)
- word转pdf实现总结
- java实现word转pdf
- java 实现word转pdf
- C#实现Word转PDF
- C# 实现Excel转Word、Word转PDF、合并PDF
- 轻松实现文档转换的word转pdf转换器
- 轻松实现文档转换的word转pdf转换器
- 介绍用编程解决Word转PDF的几种方案
- java动态生成word和PDF的几种方案
- word转PDF的方法
- PDF转WORD以及WORD转PDF的方法
- repo
- Android程序界面主题切换
- 学习计划
- ApplicatonContext注册BeanFactoryPostProcessor处理器
- 数据仓库设计基础--关系模型和多维模型
- word转pdf的最佳实现方案
- JSPServlet编码问题总结
- spring mvc注解
- hdu 3724Encoded Barcodes
- 理解数据结构
- java提供的把中文转化为ascII码(*.properties 文件配置使用)的方法
- 如果不坚持,到哪里都是放弃
- OCP-1Z0-053-V12.02-626题
- Android中contentDescription属性作用