Java后台生成pdf文件

简介: Java后台生成pdf文件

前段时间因为相关业务需求需要后台生成pdf文件,对于一直crud的程序员来说,这无疑是需要一定时间来做技术预研的。下面根据我的实践经验总结一下我是如何使用java生成pdf文件的。

根据spring mvc的设计模式,理论上来说,我们可以把pdf文件视作一个View视图,那么整个mvc模型如下图:

832f05d31a3a4d60b3ee309d8ba2f282_tplv-k3u1fbpfcp-zoom-in-crop-mark_4536_0_0_0.png

如果按照上图所示,那么我们要编写一个pdf视图解析器,这无疑是一个有难度的事情。但是把思路转换一下,我们可以先把model转换成html,再通过html转换成pdf是不是会更容易一点?

454f46428ffa4f54a9a01b19ff190cdf_tplv-k3u1fbpfcp-zoom-in-crop-mark_4536_0_0_0.png

1.如何把model转换成html?

这个问题spring mvc已经替我们解决了,thymeleaf的实现无非就是一个活生生的model转换成html的例子。

2.html如何转换成pdf?

基于IText 基于FlyingSaucer 基于WKHtmlToPdf 基于pd4ml
跨平台性 跨平台 跨平台 跨平台 跨平台
是否安装软件 需安装WKHtmlToPdf
是否收费 免费 免费 免费 收费
转换Html效率 速度快 未测 速度慢。相比URL来说,效率较慢。能忽略一些html语法或资源是否存在问题。 速度快。部分CSS样式不支持。
效果 存在样式失真问题。对html语法有一定要求 存在样式失真问题。对html语法有较高要求。 失真情况较小,大部分网页能按Chome浏览器显示的页面转换 部分CSS样式有问题。
转换URL效率 未测 未测 效率不是特别高 未测
效果 未测 未测 部分网页由于其限制,或将出现html网页不完整。 未测
优点 不需安装软件、转换速度快 不需安装软件、转换速度快 生成PDF质量高 不需要安装软件、转换速度快
缺点 对html标签严格,少一个结束标签就会报错;服务器需要安装字体 对html标签严格,少一个结束标签就会报错;服务器需要安装字体 需要安装软件、时间效率不高 对部分CSS样式不支持。
分页 图片 表格 链接 中文 特殊字符 整体样式 速度
IText 支持 支持 支持 支持 支持 支持 失真问题
FlyingSaucer 未知 未知 未知 未知 未知 未知 未知
WKHtmlToPdf 支持 支持 支持 支持 支持 支持 很好
pd4ml 支持 支持 支持 支持 支持 支持 失真问题

对比以上各类实现:

1.WKHtmlToPdf因为转换速度慢、需要安装软件的缺点被暂时排除在外;pd4ml因为是收费的,并且同样存在一些常见的样式失真问题,直接排除;

2.剩下的就是在IText和FlyingSaucer的实现方案中做选择,对比之下,选择IText作为我们的最终实现方案

【相关依赖】

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.13.2</version>
</dependency>
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf-itext5</artifactId>
    <version>9.1.22</version>
</dependency>
复制代码

【代码实现】

import com.itextpdf.text.pdf.BaseFont;
import com.zx.silverfox.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
@Slf4j
public final class HtmlUtil {
    private HtmlUtil() {
    }
    // 字体路径,放在资源目录下
    private static final String FONT_PATH = "classpath:simsun.ttc";
    public static void file2Pdf(File htmlFile, String pdfFile) throws GlobalException {
        try (OutputStream os = new FileOutputStream(pdfFile)) {
            String url = htmlFile.toURI().toURL().toString();
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(url);
            // 解决中文支持
            ITextFontResolver fontResolver = renderer.getFontResolver();
            // 获取字体绝对路径,ApplicationContextUtil是我自己写的类
            String fontPath = ApplicationContextUtil.classpath(FONT_PATH);
            fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.layout();
            renderer.createPDF(os);
        } catch (Exception e) {
            // 抛出自定义异常
            throw GlobalException.newInstance(e);
        }
    }
    public static void html2Pdf(String html, String pdfFile) throws GlobalException {
        String pdfDir = StringUtils.substringBeforeLast(pdfFile, "/");
        File file = new File(pdfDir);
        if (!file.exists()) {
            file.mkdirs();
        }
        try (OutputStream os = new FileOutputStream(pdfFile)) {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html);
            // 解决中文支持
            ITextFontResolver fontResolver = renderer.getFontResolver();
            // 获取字体绝对路径,ApplicationContextUtil是我自己写的类
            String fontPath = ApplicationContextUtil.classpath(FONT_PATH);
            fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.layout();
            renderer.createPDF(os);
        } catch (Exception e) {
            // 抛出自定义异常
            throw GlobalException.newInstance(e);
        }
    }
}
复制代码

【字体文件】

simsun.tcc   密码:rzw4

以上实现就完成了html转换成pdf的功能,后续就是model转html:

因为我使用的是springboot,所以直接使用以下依赖。小伙伴可以根据自身项目具体情况使用对应的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
复制代码

【代码实现】

import com.google.common.collect.Maps;
import com.zx.silverfox.common.exception.GlobalException;
import com.zx.silverfox.common.util.HtmlUtil;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.util.Map;
public abstract class AbstractTemplate {
    // 使用thymeleaf模版引擎
    private TemplateEngine engine;
    // 模版名称
    private String templateName;
    private AbstractTemplate() {}
    public AbstractTemplate(TemplateEngine engine,String templateName) {
        this.engine = engine;
        this.templateName=templateName;
    }
    /**
     * 模版名称
     *
     * @return
     */
    protected String templateName(){
        return this.templateName;
    }
    /**
     * 所有的参数数据
     *
     * @return
     */
    private Map<String, Object> variables(){
        // Maps是使用到了guava依赖
        Map<String, Object> variables = Maps.newHashMap();
        // 对应html模版中的template变量,取值的时候就按照“${template.字段名}”格式,可自行修改
        variables.put("template", this);
        return variables;
    };
    /**
     * 解析模版,生成html
     *
     * @return
     */
    public String process() {
        Context ctx = new Context();
        // 设置model
        ctx.setVariables(variables());
        // 根据model解析成html字符串
        return engine.process(templateName(), ctx);
    }
    public void parse2Pdf(String targetPdfFilePath) throws GlobalException {
        String html = process();
        // 通过html转换成pdf
        HtmlUtil.html2Pdf(html, targetPdfFilePath);
    }
}
复制代码

创建模版引擎

@Configuration
public class TemplateEngineConfig {
    // 注入TemplateEngine模版引擎
    @Bean
    public TemplateEngine templateEngine(){
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        // 设置模版前缀,相当于需要在资源文件夹中创建一个html2pdfTemplate文件夹,所有的模版都放在这个文件夹中
        resolver.setPrefix("/html2pdfTemplate/");
        // 设置模版后缀
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("UTF-8");
        // 设置模版模型为HTML
        resolver.setTemplateMode("HTML");
        TemplateEngine engine = new TemplateEngine();
        engine.setTemplateResolver(resolver);
        return engine;
    }
}
复制代码

因为我们的依赖是基于springboot的,所以为了不让spring-boot-starter-thymeleaf自动配置,我们需要排除相关的配置类。不想这样做的小伙伴可使用thymeleaf其他依赖,原理上都一样。

@SpringBootApplication(exclude = ThymeleafAutoConfiguration.class)
复制代码

至此,所有的技术准备都做好了,如何使用我们编写好的代码实现model转换pdf文件呢?

【示例】

import lombok.Data;
import org.thymeleaf.TemplateEngine;
import java.util.List;
@Data
public class Model extends AbstractTemplate {
    // 构造函数
    public Model(TemplateEngine engine, String templateName) {
        super(engine, templateName);
    }
    // 名称
    private String name;
    // 保险记录
    private List<InsuranceInfo> insuranceInfos; 
}
@Data
public class InsuranceInfo{
    /** 出险日期 */
    private String expirationDate;
    /** 描述 */
    private String description;
}
复制代码

【报告模版.html】

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>报告模版</title>
    <style>
      <!-- 编写css   -->
    </style>
</head>
 <!--  引入字体  -->
<body style="font-family: SimSun;">
<div class="main">
    报告模版
</div>
<div class="main2">
    <span class="heng" th:text="${template.name}">template.name</span>
    <table  class="tabletype">
      <thead>
        <tr class="recordhead">
          <th class="leaf" style="width: 80px;">出险日期</th>
          <th class="leaf" style="width: 80px;">描述</th>
        </tr>
      </thead>
      <tbody th:if="${template.insuranceInfos}">
        <tr  th:each="m,var : ${template.insuranceInfos}">
          <th class="leaf" th:text="${m.expirationDate}"></th>
          <th class="leaf" th:text="${m.description}"></th>
        </tr>
      </tbody>
  </table>
</div>
</body>
</html>
复制代码

【测试代码】

@Autowired private TemplateEngine engine;
    public void test() throws Exception {
        // 创建model,需要指定模版引擎和具体的模版,“报告模版”指的是资源目录下/html2pdfTemplate/报告模版.html文件。如果是springboot项目,那么就是在resources文件夹下面
        Model model = new Model(engine,"报告模版");
        model.setName("名称");
        List<InsuranceInfo> insuranceInfos = new ArrayList<>();
        InsuranceInfo record1 = new InsuranceInfo();
        record1.setExpirationDate("2021-01-19");
        record1.setDescription("刹车失灵");
        insuranceInfos.add(record1);
        InsuranceInfo record2 = new InsuranceInfo();
        record2.setExpirationDate("2021-03-06");
        record2.setDescription("挡风玻璃破裂");
        insuranceInfos.add(record2);
        model.setInsuranceInfos(insuranceInfos);
        //生成pdf,指定目标文件路径
        model.parse2Pdf("/home/dev/桌面/test.pdf");
    }
复制代码

根据以上理论和实践,我们已经达到了我们的目标,最终完成了数据转换成PDF文件的需求



相关文章
|
1月前
|
Java Unix Go
【Java】(8)Stream流、文件File相关操作,IO的含义与运用
Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。!但本节讲述最基本的和流与 I/O 相关的功能。我们将通过一个个例子来学习这些功能。
155 1
|
4月前
|
XML 人工智能 Java
java实现PDF 电子签章
本文介绍了使用Java将Word文档转换为PDF并添加水印、签名和盖章的方法。通过Apache POI读取Word内容,结合OpenPDF生成PDF文件,并利用PdfPageEvent接口实现页面水印与签名功能。代码示例清晰展示了转换流程及关键实现细节。
280 0
|
4月前
|
存储 Java 编译器
深入理解Java虚拟机--类文件结构
本内容介绍了Java虚拟机与Class文件的关系及其内部结构。Class文件是一种与语言无关的二进制格式,包含JVM指令集、符号表等信息。无论使用何种语言,只要能生成符合规范的Class文件,即可在JVM上运行。文章详细解析了Class文件的组成,包括魔数、版本号、常量池、访问标志、类索引、字段表、方法表和属性表等,并说明其在Java编译与运行过程中的作用。
131 0
|
4月前
|
C#
【PDF提取内容改名】批量提取PDF指定区域内容重命名PDF文件,PDF自动提取内容命名的方案和详细步骤
本工具可批量提取PDF中的合同编号、日期、发票号等关键信息,支持PDF自定义区域提取并自动重命名文件,适用于合同管理、发票处理、文档归档和数据录入场景。基于iTextSharp库实现,提供完整代码示例与百度、腾讯网盘下载链接,助力高效处理PDF文档。
618 40
|
4月前
|
编译器 Python
如何利用Python批量重命名PDF文件
本文介绍了如何使用Python提取PDF内容并用于文件重命名。通过安装Python环境、PyCharm编译器及Jupyter Notebook,结合tabula库实现PDF数据读取与处理,并提供代码示例与参考文献。
|
4月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
288 0
|
4月前
|
监控 Java API
Java语言按文件创建日期排序及获取最新文件的技术
这段代码实现了文件创建时间的读取、文件列表的获取与排序以及获取最新文件的需求。它具备良好的效率和可读性,对于绝大多数处理文件属性相关的需求来说足够健壮。在实际应用中,根据具体情况,可能还需要进一步处理如访问权限不足、文件系统不支持某些属性等边界情况。
251 14
|
5月前
|
数据采集 存储 API
Python爬虫结合API接口批量获取PDF文件
Python爬虫结合API接口批量获取PDF文件
|
5月前
|
存储 Java 数据安全/隐私保护
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
460 5
|
5月前
|
网络协议 安全 Java
实现Java语言的文件断点续传功能的技术方案。
像这样,我们就完成了一项看似高科技、实则亲民的小工程。这样的技术实现不仅具备实用性,也能在面对网络不稳定的挑战时,稳稳地、不失乐趣地完成工作。
322 0

热门文章

最新文章