我转化pdf的目的是把用户上传的文档转pdf以作在线预览功能。
第一步需要下一个Apache_OpenOffice插件,没有它实现不了转化。
默认安装,
/**
* 在此之前需先开启openoffice服务,用命令行打开cd C:\Program Files\OpenOffice.org 3\program (openoffice安装的路径)
* 输入 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
*/开启Apache_OpenOffice服务
第二步项目引入Apache_OpenOffice相关jar包
<!-- 文档 --> <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.1</version> </dependency>
jodconverter-2.2.2可以把docx,xlsx转pdf,mvn好像不能自动下来,需要手动引入
/** * */ package com.hailian.modules.credit.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ConnectException; import org.apache.commons.io.FileUtils; 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.StreamOpenOfficeDocumentConverter; /** * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) * 转化为pdf文件<br> * Office2010的没测试<br> * pdfTempPath为pdf存放地址,在项目目录下 * @date 2017-03-03 * @author jjc * */ public class Office2PDF { public static File convertFileToPdf(File sourceFile,String name,String type, String pdfTempPath) throws Exception { File pdfFile = new File(pdfTempPath+"/"+name+".pdf"); // File pdfFile = new File(pdfTempPath+"/"+name); // 临时pdf文件 if (!pdfFile.getParentFile().exists()) { pdfFile.getParentFile().mkdirs(); } InputStream inputStream = null; OutputStream outputStream = null; if (!type.equals("pdf")) { // 获得文件格式 DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry(); DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf"); DocumentFormat docFormat = formatReg.getFormatByFileExtension(type); /** * 在此之前需先开启openoffice服务,用命令行打开cd C:\Program Files\OpenOffice.org 3\program (openoffice安装的路径) * 输入 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard */ OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { // stream 流的形式 inputStream = new FileInputStream(sourceFile); outputStream = new FileOutputStream(pdfFile); connection.connect(); DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); // File outputFile = new File(pdfTempPath+"/xx.pdf"); // 假如目标路径不存在,则新建该路径 // if (!outputFile.getParentFile().exists()) { // outputFile.getParentFile().mkdirs(); // } converter.convert(inputStream, docFormat, outputStream, pdfFormat); // converter.convert(sourceFile,pdfFile); } catch (Exception e) { e.printStackTrace(); throw new Exception(e.getMessage()); } finally { if (connection != null) { connection.disconnect(); connection = null; } try { inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("转化pdf成功...."); } else { // 复制pdf文件到新的文件 FileUtils.copyFile(sourceFile, pdfFile); } return pdfFile; } public static void main(String[] args) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(connection); connection.disconnect(); } }