1.下载openoffice
openoffice的下载地址:http://www.openoffice.org/
2.安装(记住自己的安装路径,待会要用到)
3. pom.xml
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0.5</version>
</dependency>
4.文档转pdf业务层
// 文件上传根目录
@Value("${UPLOAD_URL}")
private String UPLOAD_URL;
@Value("${DOCUMENT_URL}")
private String DOCUMENT_URL;
/**
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
*
* @param inputFilePath 源文件路径,如:"e:/test.docx"
* @param outputFilePath 目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
@Override
public Map openOfficeToPDF(String inputFilePath, String outputFilePath) {
return office2pdf(inputFilePath, outputFilePath);
}
/**
* 根据操作系统的名称,获取OpenOffice 的安装目录<br>
* 如我的OpenOffice 安装在:C:/Program Files (x86)/OpenOffice 4
*
* @return OpenOffice 的安装目录
*/
private String getOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice.org3";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:/Program Files (x86)/OpenOffice 4";
} else if (Pattern