pdf根据模板导出
根据参数isAll和ids筛选所需的YDTemplateOfSpfc对象 设置默认字体以确保PDF文本的正确显示 将数据填充到PDF表单 使用iTextPDF工具生成并返回PDF文件 对生成的PDF文件进行压缩存储
发布日期:2025-05-01 23:59:18
浏览次数:12
分类:精选文章
本文共 5454 字,大约阅读时间需要 18 分钟。
如何使用iTextPDF生成PDF在实际开发中,我们需要导入以下依赖包:依赖包:- com.itextpdf:itextpdf:5.4.3- com.itextpdf:itext-asian:5.2.0PDF生成方法public boolean getPdf(String ids, HttpServletRequest request, HttpServletResponse response, String isAll, YdTemplateOfSpfc ydTemplateOfSpfc) throws Exception { List list = new ArrayList<>(); // 处理isAll参数 if (StringUtil.isNotBlank(isAll)) { EntityWrapper wrapper = new EntityWrapper<>(); // 根据申请ID过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getApplicantId())) { wrapper.where("applicant_id={0}", ydTemplateOfSpfc.getApplicantId()); } // 根据吨次年份过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getTonYear())) { wrapper.where("ton_year={0}", ydTemplateOfSpfc.getTonYear()); } // 根据卸船船名过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselName())) { wrapper.like("unloading_vessel_name", ydTemplateOfSpfc.getUnloadingVesselName()); } // 根据卸船船舶ID过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselId())) { wrapper.like("receiving_vessel_name", ydTemplateOfSpfc.getReceivingVesselName()); } // 过滤未删除的记录 wrapper.where("isdelete={0}", "0"); // 过滤未上报的状态 wrapper.where("report_state={0}", "1"); list = ydTemplateOfSpfcService.selectList(wrapper); } else { // 处理ids参数 String[] split = ids.split(","); for (int i = 0; i < split.length; i++) { String id = split[i]; // 读取对应的YDTemplateOfSpfc对象 YdTemplateOfSpfc ydTemplateOfSpfc = list.get(i); // 将对象添加到列表中 list.add(ydTemplateOfSpfc); } } // 检查列表是否为空 if (list.isEmpty()) { return false; } // 设置JAXB解析器 System.setProperty("javax.xml.parsers.documentbuilderfactory", "com.sun.org.apache.xerces.internal.jaxp.documentbuilderfactoryimpl"); try { // 读取PDF模板 PDFStamper ps = new PDFStamper(new PDFReader("src/main/resources/nantaireprinttable_model/ceshi.pdf")); // 创建输出流 FileOutputStream fos = new FileOutputStream("src/main/resources/NantaiReprintTable_pdf" + ydTemplateOfSpfc.getAnticipatedDate() + ydTemplateOfSpfc.getAndTime() + "nantaireprinttable.pdf"); // 初始化PDF表单 PDFReader reader = new PDFReader(fos); PDFStamper pdfStamper = new PDFStamper(reader, fos); // 设置默认字体 BaseFont bf = BaseFont.createFont("src/main/resources/NantaiReprintTable_zip/Fonts/simsun.ttc", 0); BaseFont baseFont = BaseFont.IDENTITY_H; pdfStamper.getAcroFields().addSubstitutionFont(bf); // 读取数据并填充到PDF表单 Map data = new HashMap<>(); data.put("fill_1", ydTemplateOfSpfc.getAnticipatedDate()); data.put("fill_2", ydTemplateOfSpfc.getAndTime()); // 处理经度和纬度格式 if (ydTemplateOfSpfc.getAnticipatedLongitude().equals("W")) { ydTemplateOfSpfc.setLongitudeNumber("-" + ydTemplateOfSpfc.getLongitudeNumber()); } if (ydTemplateOfSpfc.getAnticipatedLatitude().equals("S")) { ydTemplateOfSpfc.setLatitudeNumber("-" + ydTemplateOfSpfc.getLatitudeNumber()); } data.put("fill_3", ydTemplateOfSpfc.getLatitudeNumber()); data.put("fill_4", ydTemplateOfSpfc.getLongitudeNumber()); data.put("fill_5", ydTemplateOfSpfc.getTranshippedFishSpecies()); data.put("fill_6", ydTemplateOfSpfc.getVesselName()); data.put("fill_7", ydTemplateOfSpfc.getUnloadingVesselName()); data.put("fill_8", ydTemplateOfSpfc.getUnloadingRegistrationNumber()); data.put("fill_9", ydTemplateOfSpfc.getUnloadingRadioCallSign()); data.put("fill_10", ydTemplateOfSpfc.getUnloadingVesselFlag()); data.put("fill_11", ydTemplateOfSpfc.getUnloadingImoNumber()); data.put("fill_12", ydTemplateOfSpfc.getNameOfUnloadingVesselMaster()); data.put("fill_13", ydTemplateOfSpfc.getNationalityOfUnloadingVesselMaster()); data.put("fill_14", ydTemplateOfSpfc.getReceivingVesselName()); data.put("fill_15", ydTemplateOfSpfc.getReceivingRegistrationNumber()); data.put("fill_16", ydTemplateOfSpfc.getReceivingRadioCallSign()); data.put("fill_17", ydTemplateOfSpfc.getReceivingVesselFlag()); data.put("fill_18", ydTemplateOfSpfc.getUnloadingImoNumber()); data.put("fill_19", ydTemplateOfSpfc.getNameOfReceivingVesselMaster()); data.put("fill_20", ydTemplateOfSpfc.getNationalityOfReceivingVesselMaster()); // 将数据填充到PDF表单 for (String key : data.keySet()) { pdfStamper.getAcroFields().setField(key, data.get(key).toString()); } pdfStamper.setFormFlattening(true); System.out.println("PDF导出成功"); } catch (Exception e) { System.out.println("PDF导出失败"); e.printStackTrace(); } finally { try { ps.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } // 压缩文件 CompressUtil zipFiles(new File("src/main/resources/NantaiReprintTable_zip/"), new File("src/main/resources/NantaiReprintTable_zip/NantaiReprintTable_pdf.zip")); return true;} 以上代码实现了以下功能:
代码中主要使用了以下iTextPDF功能:
- PDFStamper:用于对PDF表单进行操作
- PDFReader:用于读取PDF模板
- BaseFont:用于设置默认字体
- AcroFields:用于PDF表单字段的操作
- EntityWrapper:用于数据库查询条件的组合
代码中还包含了对经纬度格式的处理逻辑,确保输出的PDF文件符合指定的格式要求。
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2026年06月10日 12时32分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP获取当前文件的绝对路径
2023-03-01
PHP获取当前时间、时间戳的各种格式写法汇总
2023-03-01
PHP获取当前页面的完整URL
2023-03-01
php获取数据库中数据生成json,中文乱码问题的解决方案
2023-03-01
php获取文件夹中文件的两种方法
2023-03-01
PHP获取日期的一些方法总结
2023-03-01
R2学习记录
2023-03-01
PHP获取本周的每一天的时间
2023-03-01
php获取用户真实IP和防刷机制
2023-03-01
php获取网页内容的三种方法
2023-03-01
R-CNN算法优化策略
2023-03-01
PHP规范PSR0和PSR4的理解
2023-03-01
php解析ipa包,获取logo
2023-03-01
R&Rstudio安装各种包
2023-03-02
php设置cookie,在js中如何获取
2023-03-02
php设置socket超时时间
2023-03-02
php设计模式 萨莱 pdf,PHP设计模式 建造者模式
2023-03-02
PHP设计模式之----观察者模式
2023-03-02
php设计模式之装饰器模式
2023-03-02
R&Python Data Science系列:数据处理(5)--字符串函数基于R(一)
2023-03-02