项目中有生成word文档,并支持在线预览的需求,我首先使用freemarker模板生成word文件,然后使用word转pdf的方式实现文件预览,最终找到了以下两种方法:
1、POI
引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.2</version>
</dependency>
文件转换
public static void main(String[] args) throws IOException {
String wordSourcePath = "C:\\Users\\Administrator\\Desktop\\test\\test.docx";
String pdfSourcePath = wordSourcePath.replace(".docx", ".pdf");
boolean wordToPdf = wordToPdf(wordSourcePath, pdfSourcePath);
}
public static boolean wordToPdf(String wordPath, String pdfPath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(wordPath);
XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
PdfOptions pdfOptions = PdfOptions.create();
FileOutputStream fileOutputStream = new FileOutputStream(pdfPath);
PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
fileInputStream.close();
fileOutputStream.close();
return true;
}
2、Aspose
引入依赖
word:aspose-words-15.8.0-jdk16
excel:aspose-cells-8.5.2
<!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
<!-- 中央仓库拉取不到,建议下载文件后手动安装到私服 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
需要在项目里加入一个license.xml,不然生成的pdf会有水印
license.xml示例如下:
<?xml version="1.0" encoding="utf-8"?>
<License>
<Data>
<LicensedTo>Shanghai Hudun Information Technology Co., Ltd</LicensedTo>
<EmailTo>317701809@qq.com</EmailTo>
<LicenseType>Developer OEM</LicenseType>
<LicenseNote>Limited to 1 developer.</LicenseNote>
<OrderID>140714065432</OrderID>
<UserID>266166</UserID>
<OEM>This is a redistributable license</OEM>
<Products>
<Product>Aspose.Total for .NET</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SerialNumber>ea712420-fbb2-41e7-9ede-39f7b3a43e4a</SerialNumber>
<SubscriptionExpiry>20150714</SubscriptionExpiry>
<LicenseVersion>3.0</LicenseVersion>
<LicenseInstructions>http://www.aspose.com/corporate/purchase/license-instructions.aspx</LicenseInstructions>
</Data>
<Signature>Jv8ksvWRYLPzqyQX6J5pAUk4VNc1sogN5xq3ep7Piiy0iGQXg3p8yiGILjPpyCn7G2fkpcnCWPPffoWKqELUc40CiQWsuSmvAuLpvdJCVArt41CpaHNPAUKhrpOzrruzMPWgOB/ByzBVyvUkXl5g2OYHS29gVFlCJ2Ps+p0LnIQ=</Signature>
</License>
文件转换工具类
public class WordToPdfUtil {
public static boolean wordToPdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return false;
}
FileOutputStream os = null;
try {
File file = new File(pdfPath);
os = new FileOutputStream(file);
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
private static boolean getLicense() {
boolean result = false;
try {
// license.xml放在resources根目录下
InputStream is = Test.class.getClassLoader().getResourceAsStream("\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
中文乱码
假如你使用的是Linux服务器,这个转化过程不加以配置的话,在Linux中转化后的PDF,如果带有中文是会有乱码现象的。因为Linux里面没有中文字符的样式,所以我们要做的就是将window中的中文样式导入到Linux服务器里面。
1、找到windows系统的中文样式,并打成一个包。这个位于C:\Windows\Fonts,这个文件一般来说是只读的,但我们可以直接复制一个,然后再打包,打包成一个zip包
2、传到Linux服务器
3、解压到指定文件夹
4、安装字体
经过对比,POI可以实现常规的文本转换,数据表格没有转换成功,于是选择Aspose,完美转换。