本人使用的是免费版
限制只能转3页,而且见面会有警告提示信息。而且显示中文总是有问题
pom依赖
<properties>
<itext7.version>7.1.7itext7.version>
properties>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>font-asianartifactId>
<version>7.1.7version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>kernelartifactId>
<version>${itext7.version}version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>ioartifactId>
<version>${itext7.version}version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>layoutartifactId>
<version>${itext7.version}version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>formsartifactId>
<version>${itext7.version}version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>pdfaartifactId>
<version>${itext7.version}version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>html2pdfartifactId>
<version>2.1.4version>
dependency>
<repositories>
<repository>
<id>com.e-iceblueid>
<url>https://repo.e-iceblue.cn/repository/maven-public/url>
repository>
repositories>
测试代码
public class test {
public static void main(String[] args) throws ParseException {
Document doc = new Document();
doc.loadFromFile("E:/test/123.xml");
//保存为Docx格式
doc.saveToFile("E:/test/123toDocx.docx",FileFormat.Docx);
}
}
注意事项:仅支持Windows
转换堪称良好。完美转换
pom依赖
<dependency>
<groupId>org.docx4jgroupId>
<artifactId>docx4j-JAXB-InternalartifactId>
<version>8.3.4version>
dependency>
<dependency>
<groupId>org.docx4jgroupId>
<artifactId>docx4j-JAXB-ReferenceImplartifactId>
<version>8.3.4version>
dependency>
<dependency>
<groupId>org.docx4jgroupId>
<artifactId>docx4j-JAXB-MOXyartifactId>
<version>8.3.4version>
dependency>
<dependency>
<groupId>org.docx4jgroupId>
<artifactId>docx4j-documents4j-localartifactId>
<version>8.3.4version>
dependency>
<dependency>
<groupId>com.documents4jgroupId>
<artifactId>documents4j-transformer-msoffice-excelartifactId>
<version>1.1.7version>
dependency>
测试代码
@SpringBootApplication
public class App
{
public static void main( String[] args ) throws Exception
{
SpringApplication.run(App.class, args);
String htmlFile = "E:/test/123.doc";
String pdfFile = "E:/test/testoone23.pdf";
//word(freemarker)转pdf
OutputStream os = new FileOutputStream(pdfFile);
Documents4jLocalServices exporter = new Documents4jLocalServices ();
exporter.export(new File(htmlFile), os, DocumentType.MS_WORD);
}
}
libreoffice地址:链接: https://pan.baidu.com/s/1klfCgCvXWuV0GmmdoihyDA?pwd=1234
支持linux,windows。但是freemarker生成的word文件,使用libreoffice打开不显示图片,转换为pdf后依旧不显示
pom依赖
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-coreartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-localartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-onlineartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-spring-boot-starterartifactId>
<version>4.2.2version>
dependency>
工具类
package cn.com.wy.test;
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jodconverter.DocumentConverter;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.LocalOfficeUtils;
import org.jodconverter.office.OfficeException;
import org.springframework.stereotype.Component;
/**
* office文档转PDF文档
*/
@Component
public class OfficeToPdfUtil {
static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
//E:\\LibreOffice 是安装目录
static LocalOfficeManager localOfficeManager = LocalOfficeManager.builder().officeHome("E:\\LibreOffice").install().build();
OfficeToPdfUtil() throws OfficeException {
logger.info("***启动OfficeManager***");
if(!localOfficeManager.isRunning()){
localOfficeManager.start();
}
}
/**
* @Description office转pdf
* @Param [fromPath--源文件, toPath--目标文件]
* @return void
**/
public static void convertByLocal(File fromPath,File toPath) {
try {
logger.info("***开始文件转化pdf***");
DocumentConverter converter = LocalConverter.builder().officeManager(localOfficeManager).build();
//使用本地方式转换
converter.convert(fromPath).to(toPath).execute();
logger.info("***文件转化pdf成功***");
} catch (OfficeException officeException) {
logger.error("文件转换失败,请检查");
officeException.printStackTrace();
}finally {
}
}
public static void stopServer() throws OfficeException {
// Stop the office process
localOfficeManager.stop();
logger.info("***停止OfficeManager***");
}
}
测试代码
@SpringBootApplication
public class App
{
public static void main( String[] args ) throws Exception
{
SpringApplication.run(App.class, args);
String htmlFile = "E:/test/123.doc";
String pdfFile = "E:/test/testoone23.pdf";
//word(freemarker)转pdf
OfficeToPdfUtil.convertByLocal(new File(htmlFile), new File(pdfFile));
OfficeToPdfUtil.stopServer();
}
}
openOffice地址:http://www.openoffice.org/download/index.html
freemarker生成的xml文件,直接使用下发代码即可转换为pdf
且支持windows和linux
pom依赖
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-coreartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-localartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-onlineartifactId>
<version>4.2.2version>
dependency>
<dependency>
<groupId>org.jodconvertergroupId>
<artifactId>jodconverter-spring-boot-starterartifactId>
<version>4.2.2version>
dependency>
工具类
package cn.com.wy.test;
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jodconverter.DocumentConverter;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.LocalOfficeUtils;
import org.jodconverter.office.OfficeException;
import org.springframework.stereotype.Component;
/**
* office文档转PDF文档
*/
@Component
public class OfficeToPdfUtil {
static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
//E:\\openOffice 是openOffice安装目录
static LocalOfficeManager localOfficeManager = LocalOfficeManager.builder().officeHome("E:\\openOffice").install().build();
OfficeToPdfUtil() throws OfficeException {
logger.info("***启动OfficeManager***");
if(!localOfficeManager.isRunning()){
localOfficeManager.start();
}
}
/**
* @Description office转pdf
* @Param [fromPath--源文件, toPath--目标文件]
* @return void
**/
public static void convertByLocal(File fromPath,File toPath) {
try {
logger.info("***开始文件转化pdf***");
DocumentConverter converter = LocalConverter.builder().officeManager(localOfficeManager).build();
//使用本地方式转换
converter.convert(fromPath).to(toPath).execute();
logger.info("***文件转化pdf成功***");
} catch (OfficeException officeException) {
logger.error("文件转换失败,请检查");
officeException.printStackTrace();
}finally {
}
}
public static void stopServer() throws OfficeException {
// Stop the office process
localOfficeManager.stop();
logger.info("***停止OfficeManager***");
}
}
测试代码
@SpringBootApplication
public class App
{
public static void main( String[] args ) throws Exception
{
SpringApplication.run(App.class, args);
String htmlFile = "E:/test/123.xml";
String pdfFile = "E:/test/testoone23.pdf";
//word(freemarker)转pdf
OfficeToPdfUtil.convertByLocal(new File(htmlFile), new File(pdfFile));
OfficeToPdfUtil.stopServer();
}
}