springboot根据模板生成pdf文件

前言

        最近碰到一个需求,给定一个word/pdf的模板,生成pdf文件,包含pdf文件之间的合并等操作。试了很多种方法(也可能是本人比较菜的原因),只有下面这个方法走通了,做下记录。

文件生成

1 准备模板

这里用的是word模板,生成文件用的是poi-tl,贴一下官网地址:Poi-tl Documentation

springboot根据模板生成pdf文件_第1张图片

  最终生成后的文件效果:

 springboot根据模板生成pdf文件_第2张图片

2 引入依赖

            
            
                org.apache.poi
                poi
                4.1.2
            
            
                org.apache.poi
                poi-scratchpad
                4.1.2
            
            
                org.apache.poi
                poi-ooxml
                4.1.2
            
            
                fr.opensagres.xdocreport
                xdocreport
                2.0.2
            
            
                org.apache.poi
                ooxml-schemas
                1.4
            
            
            
                com.deepoove
                poi-tl
                1.8.2
            
            
            
                org.springframework
                spring-expression
                5.3.17
            
            
            
                fr.opensagres.xdocreport
                fr.opensagres.poi.xwpf.converter.pdf-gae
                2.0.1
            
            
                org.eclipse.birt.runtime.3_7_1
                com.lowagie.text
                2.1.7
            
            
            
            
                org.apache.pdfbox
                pdfbox
                2.0.26
            
            

3 word文件生成

        Map 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、重启应用服务

你可能感兴趣的:(java)