springboot根据模板生成pdf文件

前言

        最近碰到一个需求,给定一个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、重启应用服务

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟程序员a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值