前言
最近碰到一个需求,给定一个word/pdf的模板,生成pdf文件,包含pdf文件之间的合并等操作。试了很多种方法(也可能是本人比较菜的原因),只有下面这个方法走通了,做下记录。
文件生成
1 准备模板
这里用的是word模板,生成文件用的是poi-tl,贴一下官网地址:Poi-tl Documentation
最终生成后的文件效果:
2 引入依赖
<!-- POI工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<!--导出word poi-tl-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.8.2</version>
</dependency>
<!--Sping表达式为了在模板标签中使用SpEL表达式-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.17</version>
</dependency>
<!--word转pdf start-->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>com.lowagie.text</artifactId>
<version>2.1.7</version>
</dependency>
<!--word转pdf end-->
<!--pdf合并 start-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.26</version>
</dependency>
<!--pdf合并 end-->
3 word文件生成
Map<String, Object> params = new HashMap<>();
params.put("companyName", "其过变从学");
params.put("unionCompanyName", "完精元");
params.put("currentDate", "2022年08月");
XWPFTemplate template = XWPFTemplate.compile("d:/template.docx").render(params);
FileOutputStream out = new FileOutputStream("d:/output.docx");
template.write(out);
out.flush();
out.close();
template.close();
4 word转pdf
FileInputStream fileInputStream = new FileInputStream("d:\\output.docx");
XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
PdfOptions pdfOptions = PdfOptions.create();
FileOutputStream fileOutputStream = new FileOutputStream("d:\\output.pdf");
PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
fileInputStream.close();
fileOutputStream.close();
5 pdf合并
File file1 = new File("d:/output.pdf");
PDDocument doc1 = PDDocument.load(file1);
File file2 = new File("d:/1.pdf");
PDDocument doc2 = PDDocument.load(file2);
File file3 = new File("d:/2.pdf");
PDDocument doc3 = PDDocument.load(file3);
PDFMergerUtility pdfMerger = new PDFMergerUtility();
PDDocument pdDocument = new PDDocument();
pdfMerger.appendDocument(pdDocument, doc1);
pdfMerger.appendDocument(pdDocument, doc2);
pdfMerger.appendDocument(pdDocument, doc3);
pdDocument.save("d:/merged.pdf");
doc1.close();
doc2.close();
doc3.close();
注:通过这种方式生成的pdf在Windows系统是没问题的,但是在Linux系统会出现中文不显示问题,解决方式如下:
a、将C:\Windows\Fonts 所有字体全部压缩成zip包(allfont.zip)
b、将压缩包拷贝到linux服务器上的 /usr/share/fonts目录
c、unzip allfont.zip 解压文件
d、使用命令刷新到缓存中:执行命令:fc-cache -fv
e、重启应用服务