Java 根据模板导出PDF

打印 上一主题 下一主题

主题 546|帖子 546|积分 1638

目录

前言

本文为我搜集的根据模板导出PDF的方法整理而来,所以贴了好多帖子的链接。有的方法仅适合特殊的业务场景,可以根据业务需求选择合适的方法。写的不好请轻喷。
思路一:直接导出pdf

使用itext模板导出pdf

思路二:先导出word再转成pdf

1)导出word

2)word转pdf

最终方案

docx4j  spire.doc.free + freemarker

<ol>模板准备
将占位变量名写在模板 testTemplate.docx的对应位置上,用 ${}包起来。

把复制一份副本,将副本 .docx 的后缀名重命名为 .zip。解压后找到 /word/document.xml,编译器打开文件,代码格式化后,对比例如 ${repliedUserId} 的占位参数是否被拆开(如果拆开需手动修改),修改名字为 testDocument.xml。
将源模板 testTemplate.docx 和 testDocument.xml 放到相应位置。
maven依赖
  1.     <dependencies>
  2.         
  3.         <dependency>
  4.             <groupId>org.freemarker</groupId>
  5.             <artifactId>freemarker</artifactId>
  6.             <version>2.3.22</version>
  7.         </dependency>
  8.         
  9.         <dependency>
  10.             <groupId>org.docx4j</groupId>
  11.             <artifactId>docx4j-JAXB-Internal</artifactId>
  12.             <version>8.2.4</version>
  13.         </dependency>
  14.         <dependency>
  15.             <groupId>org.docx4j</groupId>
  16.             <artifactId>docx4j-export-fo</artifactId>
  17.             <version>8.2.4</version>
  18.         </dependency>
  19.         
  20.         
  21.         <dependency>
  22.             <groupId>e-iceblue</groupId>
  23.             <artifactId>spire.doc.free</artifactId>
  24.             <version>5.2.0</version>
  25.         </dependency>
  26.     </dependencies>
  27.     <repositories>
  28.         <repository>
  29.             <id>com.e-iceblue</id>
  30.             <name>e-iceblue</name>
  31.             <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
  32.         </repository>
  33.     </repositories>
复制代码
Controller
  1. @PostMapping("/pdfExport")
  2. public ResponseEntity exportPdf(@RequestParam Map<String, Object> params) {
  3.     try {
  4.         // 查找业务数据
  5.         TestEntity testEntity = testService.querySheet(params);
  6.         // 格式转换时的暂存文件名
  7.         String fileUuid = UUID.randomUUID().toString().replaceAll("-", "");
  8.         String toDocxPath = "E://project//test//ToPDF//word//" + fileUuid + ".docx";
  9.         String toPdfPath = "E://project//test//ToPDF//pdf//" + fileUuid + ".pdf";
  10.         String toXmlPath = "E://project//test//ToPDF//xml//" + fileUuid + ".xml";
  11.         String docxTemplate = "E://project//test//ToPDF//template//testTemplate.docx";
  12.         // .xml转.docx(testDocument.xml表示在项目的相对路径下)
  13.         XmlToDocx.toDocx("testDocument.xml",docxTemplate, toXmlPath, toDocxPath, testEntity);
  14.         // .docx转.pdf
  15.         WordToPdf.docxToPdf(toDocxPath, toPdfPath);
  16.         // 下载pdf并删除本地pdf
  17.         ResponseEntity response = WordToPdf.downloadPdf("这是PDF的名字啊", toPdfPath);
  18.         return response;
  19.     } catch (Exception e) {
  20.         throw new BusinessException("下载PDF失败!" + e.getMessage());
  21.     }
  22. }
复制代码
XmlToDocx类
[code]import java.io.*;import java.util.Enumeration;import java.util.Map;import java.util.zip.ZipEntry;import java.util.zip.ZipException;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;/** * 其实docx属于zip的一种,这里只需要操作word/document.xml中的数据,其他的数据不用动 * * @author * */public class XmlToDocx {    /**     *     * @param xmlTemplate xml的文件名     * @param docxTemplate docx的路径和文件名(.docx模板)     * @param xmlTemp  填充完数据的临时xml     * @param toFilePath  目标文件名     * @param object  需要动态传入的数据     */    public static void toDocx(String xmlTemplate, String docxTemplate, String xmlTemp, String toFilePath, Object object)  {        try {            // 1.object是动态传入的数据            // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开//            Writer w1 = new OutputStreamWriter(new FileOutputStream(xmlTemp), "gb2312");            Writer w1 = new OutputStreamWriter(new FileOutputStream(xmlTemp), "utf-8");            // 2.把object中的数据动态由freemarker传给xml            XmlTplUtil.process(xmlTemplate, object, w1);            // 3.把填充完成的xml写入到docx中            XmlToDocx xtd = new XmlToDocx();            File xmlTempFile = new File(xmlTemp);            xtd.outDocx(xmlTempFile, docxTemplate, toFilePath);            // 删除临时xml文件            xmlTempFile.delete();        }catch (Exception e) {            e.printStackTrace();        }    }    /**     *     * @param documentFile 动态生成数据的docunment.xml文件     * @param docxTemplate docx的模板     * @param toFilePath  需要导出的文件路径     * @throws ZipException     * @throws IOException     */    public void outDocx(File documentFile, String docxTemplate, String toFilePath) throws ZipException, IOException {        try {            File docxFile = new File(docxTemplate);            ZipFile zipFile = new ZipFile(docxFile);            Enumeration

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表