Java利用itextpdf实现pdf文件生成

ops/2024/10/15 12:20:27/

前言

最近公司让写一个数据页面生成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;@Overridepublic 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名称// 参数四:responsepdfGenerator.generate("overseaAssistance.html", overseaVo, "2.pdf", response);
}

四、结果

得到自己想要的pdf文件
在这里插入图片描述

测试数据

java_64">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;@Overridepublic 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 /> Insuranceperiod</td><td colspan="5">${period}</td></tr></table></div><div class="signature"><br /> <br /><table class="signature_table" style="border: 0; width: 100%;"><tr><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">总经理签名:<br /> <span style="font-size:10px">Authorized Signature</span></th><tdstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;">公司签章:<br /><span style="font-size:10px">Company Stamp</span></th><tdstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 17%;"></td><thstyle="border: 0; font-size: 12px; font-weight: bold; text-align: left; width: 16%;">签发日期:<br /><span style="font-size:10px">Issue Date</span></th><tdstyle="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>

http://www.ppmy.cn/ops/125935.html

相关文章

3.Linux中安装redis及环境搭建

文章目录 1.在Ubuntu中安装redis2.在Centos中安装Redis 5(不建议&#xff0c;现在yum仓库已经停止维护)3.Ubuntu中安装mysql4.Ubuntu中安装java85.Ubuntu中启动Java程序6.环境搭建及介绍 大家好&#xff0c;我是晓星航。今天为大家带来的是 Linux中安装redis 相关的讲解&#x…

slab 缓存以及slabtop 命令学习

一、slab 缓存介绍 1.1、什么是slab 缓存 SLAB缓存是Linux内核中用于优化内存分配和管理的一种机制&#xff0c;特别针对频繁分配和释放的固定大小的小对象。它是基于 通用内存分配器(如伙伴系统) 之上的一个中间层&#xff0c;旨在通过减少分配和释放小对象的开销、降低内存…

2025 -中医儿科学 -考研-执业

第1章 中医儿科学发展简史 1.据我国古代文献记载&#xff0c;最早有小儿医的时期是&#xff1a;( ) [单选] A.春秋战国至两汉 B.秦至两汉 C.隋唐 D.唐朝 E.宋朝 正确答案: A 2.我国最早记载婴儿病的医书是&#xff1a;( ) [单选] A.《内经》 B.《千金方》 C.《颅囟经…

SRAM中的bit-write mask 和 word-write mask选项

1. 概念 bit-write mask&#xff1a; 定义&#xff1a;bit-write mask 允许在写操作中对单个字的每个位&#xff08;bit&#xff09;进行独立的控制。即&#xff0c;在写入操作时&#xff0c;特定的位可以被屏蔽&#xff0c;从而只修改需要变更的位&#xff0c;而不影响其它未…

JVM 内存区域 堆

堆是JVM中相当核心的内容&#xff0c;因为堆是JVM中管理的最大一块内存区域&#xff0c;大部分的GC也发生在堆区&#xff0c;那接下来就让深入地探究一下JVM中的堆结构。 需要明确&#xff0c;一个JVM实例只存在一个堆内存&#xff0c;堆区在JVM启动的时候就被创建&#xff0c…

微信小程序-WXS

文章目录 微信小程序-WXS概述语法文档内联WXS脚本外联WXS脚本 微信小程序-WXS 概述 WXS&#xff08;WeiXin Script&#xff09;是小程序的一套脚本语言&#xff0c;可以结合 WXML 构建出页面结构。 WXS 的应用场景是“过滤器”&#xff0c;所谓的过滤器是指在渲染数据之前&a…

使用js和canvas实现简单的网页打砖块小游戏

玩法介绍 点击开始游戏后&#xff0c;使用键盘上的←→控制移动&#xff0c;小球会不停移动&#xff0c;板子触碰小球时会反弹&#xff0c;碰撞到砖块时会摧毁砖块&#xff0c;如果没有用板子接住小球就游戏失败 代码实现 代码比较简单&#xff0c;直接阅读注释即可&#x…

Java比较两个Excel是否内容一致

领导每天让比较两个Excel中的内容&#xff0c;为了节省工作效率多摸鱼&#xff0c;就写了个java接口&#xff0c;通过上传两个文件 进行代码比较得到详细的比较结果(这个需要自己根据日志二开) 目前只实现了比较功能 话不多说直接上代码&#xff0c;具体看注释 package com.yx…