前言

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

思路一:直接导出pdf

使用itext模板导出pdf


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

1)导出word

2)word转pdf


最终方案

docx4j spire.doc.free + freemarker

  1. 模板准备

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

    把复制一份副本,将副本 .docx 的后缀名重命名为 .zip。解压后找到 /word/document.xml,编译器打开文件,代码格式化后,对比例如 ${repliedUserId} 的占位参数是否被拆开(如果拆开需手动修改),修改名字为 testDocument.xml。

    将源模板 testTemplate.docx 和 testDocument.xml 放到相应位置。

  2. maven依赖

        <dependencies>
    <!-- 动态生成word-->
    <dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.22</version>
    </dependency> <!-- docx转pdf-->
    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-Internal</artifactId>
    <version>8.2.4</version>
    </dependency>
    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-export-fo</artifactId>
    <version>8.2.4</version>
    </dependency> <!-- https://www.e-iceblue.cn/spiredocforjava/spire-doc-for-java-program-guide-content.html-->
    <!-- https://mvnrepository.com/artifact/e-iceblue/spire.doc.free -->
    <dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
    </dependency>
    </dependencies> <repositories>
    <repository>
    <id>com.e-iceblue</id>
    <name>e-iceblue</name>
    <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
    </repositories>
  3. Controller

    @PostMapping("/pdfExport")
    public ResponseEntity exportPdf(@RequestParam Map<String, Object> params) {
    try {
    // 查找业务数据
    TestEntity testEntity = testService.querySheet(params);
    // 格式转换时的暂存文件名
    String fileUuid = UUID.randomUUID().toString().replaceAll("-", "");
    String toDocxPath = "E://project//test//ToPDF//word//" + fileUuid + ".docx";
    String toPdfPath = "E://project//test//ToPDF//pdf//" + fileUuid + ".pdf";
    String toXmlPath = "E://project//test//ToPDF//xml//" + fileUuid + ".xml";
    String docxTemplate = "E://project//test//ToPDF//template//testTemplate.docx"; // .xml转.docx(testDocument.xml表示在项目的相对路径下)
    XmlToDocx.toDocx("testDocument.xml",docxTemplate, toXmlPath, toDocxPath, testEntity);
    // .docx转.pdf
    WordToPdf.docxToPdf(toDocxPath, toPdfPath); // 下载pdf并删除本地pdf
    ResponseEntity response = WordToPdf.downloadPdf("这是PDF的名字啊", toPdfPath);
    return response;
    } catch (Exception e) {
    throw new BusinessException("下载PDF失败!" + e.getMessage());
    }
    }
  4. XmlToDocx类

    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<? extends ZipEntry> zipEntrys = zipFile.entries();
    ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(toFilePath));
    int len = -1;
    byte[] buffer = new byte[1024];
    while (zipEntrys.hasMoreElements()) {
    ZipEntry next = zipEntrys.nextElement();
    InputStream is = zipFile.getInputStream(next);
    // 把输入流的文件传到输出流中 如果是word/document.xml由我们输入
    zipout.putNextEntry(new ZipEntry(next.toString()));
    if ("word/document.xml".equals(next.toString())) {
    InputStream in = new FileInputStream(documentFile);
    while ((len = in.read(buffer)) != -1) {
    zipout.write(buffer, 0, len);
    }
    in.close();
    } else {
    while ((len = is.read(buffer)) != -1) {
    zipout.write(buffer, 0, len);
    }
    is.close();
    }
    }
    zipout.close(); } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
  5. WordToPdf类

    import org.apache.commons.io.IOUtils;
    import org.docx4j.Docx4J;
    import org.docx4j.fonts.IdentityPlusMapper;
    import org.docx4j.fonts.Mapper;
    import org.docx4j.fonts.PhysicalFonts;
    import org.docx4j.openpackaging.exceptions.Docx4JException;
    import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity; import java.io.*;
    import java.net.URLEncoder; public class WordToPdf { /**
    * (Docx4J).docx转.pdf(当docx的一行全是英文以及标点符号时转换的Pdf那一行会超出范围 https://segmentfault.com/q/1010000043372748)
    * @param docxPath docx文件路径
    * @param pdfPath 输出的pdf文件路径
    * @throws Exception
    */
    @Deprecated
    public static boolean docxToPdf(String docxPath, String pdfPath) throws Exception {
    FileOutputStream out = null;
    try {
    File docxfile = new File(docxPath);
    WordprocessingMLPackage pkg = Docx4J.load(docxfile);
    Mapper fontMapper = new IdentityPlusMapper();
    fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
    fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
    fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
    fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
    fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
    fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
    fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
    fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
    fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
    fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
    fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
    fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
    fontMapper.put("等线", PhysicalFonts.get("SimSun"));
    fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
    fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
    fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
    fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
    fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
    fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
    fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
    fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
    fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
    fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
    fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
    pkg.setFontMapper(fontMapper); out = new FileOutputStream(pdfPath);
    //docx4j docx转pdf
    FOSettings foSettings = Docx4J.createFOSettings();
    // foSettings.setWmlPackage(pkg);
    foSettings.setOpcPackage(pkg);
    Docx4J.toFO(foSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL);
    // Docx4J.toPDF(pkg, out);
    // 删除源.docx文件
    docxfile.delete();
    return true; // } catch (FileNotFoundException e) {
    // e.printStackTrace();
    // } catch (Docx4JException e) {
    // e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    return false;
    } finally {
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    } /**
    * https://www.e-iceblue.cn/spiredocforjavaconversion/java-convert-word-to-pdf.html
    * (spire.doc.free:
    * 免费版有篇幅限制。在加载或保存 Word 文档时,要求 Word 文档不超过 500 个段落,25 个表格。
    * 同时将 Word 文档转换为 PDF 和 XPS 等格式时,仅支持转换前三页。)
    * (spire.doc.free)word转pdf
    * @param wordInPath word输入路径
    * @param pdfOutPath Pdf输出路径
    * @return
    */
    public static boolean convertWordToPdf(String wordInPath, String pdfOutPath) {
    try {
    //实例化Document类的对象
    Document doc = new Document();
    //加载Word
    doc.loadFromFile(wordInPath);
    //保存为PDF格式
    doc.saveToFile(pdfOutPath, FileFormat.PDF);
    return true;
    } catch (Exception e) {
    // e.printStackTrace();
    return false;
    } finally {
    // 删除源word文件
    File docxfile = new File(wordInPath);
    if (docxfile.exists()) {
    docxfile.delete();
    }
    }
    }
    }

Java 根据模板导出PDF的更多相关文章

  1. java根据模板导出PDF(利用itext)

    一.制作模板     1.下载Adobe Acrobat 9 Pro软件(pdf编辑器),制作模板必须使用该工具. 2.下载itextpdf-5.5.5.jar.itext-asian-5.2.0.j ...

  2. java根据模板导出pdf

    在网上看了一些Java生成pdf文件的,写的有点乱,有的不支持写入中文字体,有的不支持模板,有的只是随便把数据放里面生成文件,完全不考虑数据怎样放置的以及以后的维护性,想想还是自己总结一个完全版的导出 ...

  3. java根据模板导出PDF详细教程

    原文:https://blog.csdn.net/pengyufight/article/details/75305128 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word ...

  4. Java利用模板生成pdf并导出

    1.准备工作 (1)Adobe Acrobat pro软件:用来制作导出模板 (2)itext的jar包 2.开始制作pdf模板 (1)先用word做出模板界面 (2)文件另存为pdf格式文件 (3) ...

  5. java通过freemarker模板导出pdf

    需求:将网页内容导出为pdf文件,其中包含文字,图片,echarts图 原理:利用freemarker模板与数据渲染所得到的html内容,通过ITextRenderer对象解析html内容生成pdf ...

  6. java模板导出PDF

    本次完善综合特点: 一对一,点对点的给对应的地方写值,比如模板里面放了个name标识,在程序里把“张三”赋给name,那么输出的pdf里面name的地方就变成了张三,准确方便快捷 支持中文,可以使用自 ...

  7. java利用itext导出pdf

    项目中有一功能是导出历史记录,可以导出pdf和excel,这里先说导出pdf.在网上查可以用那些方式导出pdf,用itext比较多广泛. 导出pdf可以使用两种方式,一是可以根据已有的pdf模板,进行 ...

  8. java根据模板生成pdf

    原文链接:https://www.cnblogs.com/wangpeng00700/p/8418594.html 在网上看了一些Java生成pdf文件的,写的有点乱,有的不支持写入中文字体,有的不支 ...

  9. Java按模板导出Excel———基于Aspose实现

    目录 开发环境 先看效果 引入jar包 校验许可证 导出方法 测试结果 占位符 开发环境 jdk 1.8 Maven 3.6 SpringBoot 2.1.4.RELEASE aspose-cells ...

  10. Java无模板导出Excel,Apache-POI插件实现

    开发环境 jdk 1.8 Maven 3.6 Tomcat 8.5 SpringBoot 2.1.4.RELEASE Apache-POI 3.6 Idea 注意: 我是在现有的基于SpringBoo ...

随机推荐

  1. 基于Vue3+TS的Monorepo前端项目架构设计与实现

    写在前面 你好,我是前端程序员鼓励师岩家兴!去年在另一个项目https://juejin.cn/post/7121736546000044046中,我向读者朋友们介绍了结合npm包管理工具yarn作v ...

  2. 滴水 1.c++类 this指针 笔记+指针

    1.结构体带参数传递 将结构体的数据压入堆栈中 然后进入函数内进行处理 2.结构体指针传递 将this指针压入进去 来查找 a b数据 3.函数可以放在结构体里面,也可以放在结构体外面 放入进去后 但 ...

  3. TCP/IP协议(3): Wi-Fi(IEEE 802.11) 协议 —— 构成无线局域网的基本协议

    TCP/IP协议(3): Wi-Fi(IEEE 802.11) 协议 -- 构成无线局域网的基本协议 关于 Wi-Fi(IEEE 802.11) 协议 关于 IEEE 802.11 IEEE 802. ...

  4. 好用的liunx可视化管理工具

    官网: https://www.terminal.icu/ 本地连接需要点击软件右上角切换线路到本地连接

  5. PostgreSQL 按拼音排序 - convert to GBK/EUC_CN coding

    背景 国内的应用,在文本排序上基本都是按照拼音来进行排序的. 在不同的字符集中,汉字的编码可能不一样,比如UTF8和GBK,其中GBK是按拼音的顺序进行编码的,而UTF8则不是. 所以如果你的数据库使 ...

  6. RocketMQ - 消费者消费方式

    RocketMQ的消费方式包含Pull和Push两种 Pull方式:用户主动Pull消息,自主管理位点,可以灵活地掌控消费进度和消费速度,适合流计算.消费特别耗时等特殊的消费场景.缺点也显而易见,需要 ...

  7. Css3中有关的 @media 媒体查询相关的知识

    1,书写格式 @media语法: @media[mediatype][and|not|only]([mediafeature]){ /*css code*/ } 常用mediatype all     ...

  8. Word12 财务部制作本年年度报告office真题

    1.根据题目一的要求,打开素材文件,点击[文件]-[另存为],选择[当前文件夹],命名为Word. 2.根据题目二的要求,在[开始]里点击[样式]的右下角,打开样式窗口,勾选[显示预览],选中文字,鼠 ...

  9. Deer_GF之UIButtonSuper

    Deer_GF之UIButtonSuper介绍 待完善...

  10. vue 一次显示多张图片的轮播图

    1. npm i vue-slick-carousel<template> <div> <div class="activities "> &l ...