Java利用itextpdf实现pdf文件生成

前言

最近公司让写一个数据页面生成pdf的功能,找了一些市面代码感觉都太麻烦,就自己综合性整合了一个便捷的工具类,开发只需简单组装数据直接调用即可快速生成pdf文件。望大家一起学习!!!

代码获取方式:

  1. 资源下载
  2. 后台私信(一键三连哦!!!)

二、前期准备

1、html模版(放置接口所在项目的resourcess/templates/

需要准备一个要看到的pdf模版,利用html代码形式简单输出,其中需要动态填充的地方需要用变量进行填充

比如页面显示:

姓名:韩云中

性别:男

复制代码
<div>
    姓名:${name}
    性别:${sex}
</div>

2、数据实体

AbstractDocumentVo 必须继承 会有个findPrimaryKey方法需要实现,return一个你这条数据的标识即可

实体字段名称必须与html${}内容一致

复制代码
public class User extends AbstractDocumentVo {

    private String name;
    
    private String sex;

    @Override
    public String findPrimaryKey() {
        // 数据标识  id或则其它均可
        return this.name;
    }
}

三、代码开发

实现接口

复制代码
@GetMapping("/testCreatePdf")
public void testCreatePdf(HttpServletResponse response) {

    // 方式一:前端直接给你传递这个对象
    // 方式二:通过前端传递的标识,自行去库中进行数据获取
    // ** 两种方式都需要保证html用到的字段不能存在null 不然报错
    User user = new User();
    user.setName("");
    user.setSex("");
    
    // 生成pdf路径
    PdfDocumentGenerator pdfGenerator = new PdfDocumentGenerator();
    // 生成pdf  
    // 参数一:classpath中templates下对应要用的模版名称 
    // 参数二:模板数据 
    // 参数三:生成pdf名称
    // 参数四:response
    pdfGenerator.generate("overseaAssistance.html", overseaVo, "2.pdf", response);
}

四、结果

得到自己想要的pdf文件

测试数据

java实体

复制代码
package com.yxy.aob.controller;

import com.yxy.common.core.utils.file.pdf.AbstractDocumentVo;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @Description:
 * @Author: Hyz
 * @Date: 2024/10/11 11:22
 * @Version:1.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class OverseaVo extends AbstractDocumentVo {

    /**
     * 标识
     */
    private String policyNo;
    /**
     * 投保人姓名
     */
    private String holderName;
    /**
     * 投被保人关系
     */
    private String relation;
    /**
     * 投保人联络地址
     */
    private String holderAdress;
    /**
     * 投保人邮编
     */
    private String holderPostCode;

    /**
     * 被保险人姓名
     */
    private String insuredName;
    /**
     * 被保险人姓名拼音
     */
    private String insuredPingyinName;
    /**
     * 被保险人护照号码
     */
    private String insuredPassportNo;
    /**
     * 被保险人性别
     */
    private String insuredSex;
    /**
     * 被保险人出生日期
     */
    private String insuredBirthday;
    /**
     * 被保险人电话
     */
    private String insuredPhone;

    /**
     * 被保险人证件号码
     */
    private String insuredIDNo;

    /**
     * 前往国家或地区
     */
    private String destination;

    /**
     * 受益人姓名
     */
    private String beneficiaryName;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 保险期间
     */
    private String period;

    /**
     * 境外意外伤害或残疾保额
     */
    private String accidentalSumInsured;

    /**
     * 紧急救援医疗保额
     */
    private String emergencySumInsured;

    /**
     * 附加境外紧急救援医保额
     */
    private String medicalSumInsured;

    /**
     * 总保费
     */
    private String premium;
    /**
     * 签发日期
     */
    private String issueDate;

    /**
     * 省份
     */
    private String branchName;

    /**
     * 合作公司名称
     */
    private String companyName;

    @Override
    public String findPrimaryKey() {
        return this.policyNo;
    }
}

html模版

复制代码
<html>
<head>
    <title></title>
    <style type="text/css">
       body {
          margin-left: 45px;
          margin-right: 45px;
          font-family: Arial Unicode MS;
          font-size: 10px;
       }

       table {
          margin: auto;
          width: 100%;
          border-collapse: collapse;
          border: 1px solid #444444;
       }

       th,td {
          border: 1px solid #444444;
          font-size: 10px;
          margin-left: 5px;
       }

       .mcContent {
          line-height: 180%;
          padding: 20px;
       }

       .logo {
          text-align: center;
       }

       .title {
          text-align: center;
          font-weight: bold;
          font-size: 20px;
       }

       .notes {
          font-weight: normal;
          margin-left: 5px;
          margin-right: 5px;
          line-height: 18px;
       }

       .text_content {
          margin-left: 5px;
          margin-right: 5px;
          line-height: 18px;
       }

       .sum_insured_first_row {
          width: 20%;
       }

       .sum_insured_span {
          font-size: 10px;
       }

       .special_agreements_div {
          page-break-before: always;
          font-size: 14px;
          margin-top: 20px;
       }

       .special_agreements_div .special_agreements {
          font-size: 18px;
          font-weight: bold;
       }

       .title_right {
          width: 100%;
          margin: 0 auto;
       }

       .title_right p {
          text-align: left;
          margin: 0;
          margin-left: 50%;
          padding: 0;
       }

       @page {
          size: 8.5in 11in;
       @
       bottom-center
       {
          content
          :
                "page "
                counter(
                      page
                )
                " of  "
                counter(
                      pages
                );
       }

       .signature {
          margin: 0 auto;
          clear: both;
          font-size: 16px;
          font-weight: bold;
       }

       .signature_table {
          /*     font-size: 16px; */
          font-weight: bold;
       }

    </style>
</head>
<body>
<div>
    <div class="title">
       测试PDF生成--
       <p>测试单号:${policyNo}</p>
    </div>

    <div class="insurance_info">
       <table class="insurance_info_table" cellpadding="0" cellspacing="0"
             width="100%">
          <tr>
             <td width="20%" colspan="3">投保人<br /> Policyholder
             </td>
             <td width="43%" colspan="3">${holderName}<br /></td>
             <td width="15%">与被保险人关系<br /> Relationship with the Insured
             </td>
             <td >${relation}</td>
          </tr>
          <tr>
             <td width="20%" colspan="3">联络地址<br /> Correspondence Address
             </td>
             <td width="43%" colspan="3">${holderAdress}</td>
             <td width="15%">邮编<br /> Postal Code
             </td>
             <td >${holderPostCode}</td>
          </tr>
          <tr class="td_width1">
             <td width="20%" colspan="3">被保险人姓名<br /> Name of the Insured
             </td>
             <td width="13%">${holderName}</td>
             <td width="10%">(拼音)<br /> (Pinyin)
             </td>
             <td width="18%">${insuredPingyinName}</td>
             <td width="15%">护照号码<br /> Passport No
             </td>
             <td>${insuredPassportNo}</td>
          </tr>
          <tr>
             <td width="5%">性别<br /> Sex
             </td>
             <td width="5%">${insuredSex}</td>
             <td width="10%">出生日期<br /> Date of Birth
             </td>
             <td width="13%">${insuredBirthday}</td>
             <td width="10%">电话<br /> Telephone No.
             </td>
             <td width="18%">${insuredPhone}</td>
             <td width="15%">证件号码 <br />ID No.</td>
             <td >${insuredIDNo}</td>
          </tr>
          <tr>
             <td colspan="3">请详细列明前往国家或地区<br /> Destination
             </td>
             <td colspan="3">${destination}</td>
             <td>受益人姓名<br /> Beneficiary
             </td>
             <td>${beneficiaryName}</td>
          </tr>
          <tr>
             <td class="address_class" colspan="3">备注 <br /> Remarks
             </td>
             <td colspan="5">${remarks}</td>
          </tr>
          <tr>
             <td class="address_class" colspan="3">保险期间 <br /> Insurance
                period
             </td>
             <td colspan="5">${period}</td>
          </tr>
       </table>
    </div>

    <div class="signature">
       <br /> <br />
       <table class="signature_table" style="border: 0; width: 100%;">
          <tr>
             <th
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">总经理签名:
                <br /> <span style="font-size:10px">Authorized Signature</span>
             </th>
             <td
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td>
             <th
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">公司签章:
                <br /><span style="font-size:10px">Company Stamp</span>
             </th>
             <td
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td>
             <th
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 16%;">签发日期:<br />
                <span style="font-size:10px">Issue Date</span>
             </th>
             <td
                   style="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 16%;">${issueDate}</td>
          </tr>
       </table>
    </div>
    <div class="text_content">
       <br /> 我从来不是那样的人,不能耐心地拾起一地碎片,把它们凑合在一起,然后对自己说,这个修补好了的东西,跟新的完全一样。一样东西破碎了就是破碎了,我宁愿记住它最好时的模样,而不想把它修补好,然后终生看着那些碎了的地方。
    </div>

</div>
</body>
</html>
相关推荐
四谎真好看25 分钟前
Java 黑马程序员学习笔记(进阶篇18)
java·笔记·学习·学习笔记
桦说编程31 分钟前
深入解析CompletableFuture源码实现(2)———双源输入
java·后端·源码
java_t_t32 分钟前
ZIP工具类
java·zip
lang201509281 小时前
Spring Boot优雅关闭全解析
java·spring boot·后端
pengzhuofan2 小时前
第10章 Maven
java·maven
百锦再2 小时前
Vue Scoped样式混淆问题详解与解决方案
java·前端·javascript·数据库·vue.js·学习·.net
刘一说2 小时前
Spring Boot 启动慢?启动过程深度解析与优化策略
java·spring boot·后端
壹佰大多3 小时前
【spring如何扫描一个路径下被注解修饰的类】
java·后端·spring
冬夜戏雪3 小时前
基于rapidocr 的文档解析(pdf转md)工具(已部署)
pdf·运维开发
百锦再3 小时前
对前后端分离与前后端不分离(通常指服务端渲染)的架构进行全方位的对比分析
java·开发语言·python·架构·eclipse·php·maven