java整合itext pdf实现自定义PDF文件格式导出

springBoot结合itext pdf实现自定义PDF文件格式导出

背景需求:

使用PDF导出指定人员对应周次的打卡记录,每周对应星期几打卡过就打"√"。

如下图:

1、导入依赖

导入itextpdf依赖

XML 复制代码
<!--       itex PDF -->
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itextpdf</artifactId>
  <version>5.5.13.3</version>
</dependency>

2、使用代码自定义导出

2.1 定义pdf数据vo对象
java 复制代码
    // vo对象
    @Data
	class DataVo {
		/**
		 * 周次
		 */
		private String week;

		/**
		 * 打卡次数
		 */
		private int count;

		/**
		 * 周几
		 */
		private List<Integer> days;
	}
2.2 itextpdf相关代码

定义表格的列数,同时定义每列的大小或者格式,然后再依次填充每个单元格数据

注意:需要保证填充每个单元格,即使没数据也要填充空的数据,否则出现文件格式不对

java 复制代码
    // itext pdf文件构建
	@PostMapping("/pdfExport")
	public void exportPdf(HttpServletResponse response, MyAttendanceStatisticsDto dto) {
		// 封装好的业务数据
		List<DataVo> vos = new ArrayList<>();
		for (int i = 1; i <= 5; i++) {
			DataVo vo = new DataVo();
			vo.setWeek("第"+i+"周");
			vo.setCount(3);
			vo.setDays(CollUtil.newArrayList(i,6,7));
			vos.add(vo);
		}
        
		if(CollUtil.isNotEmpty(vos)) {
			Rectangle pageSize = PageSize.A4.rotate();
			Document document = new Document(pageSize);

			try {
				String title = "文件名"+ RandomUtil.randomString(5);
				String fileName = URLEncoder.encode("文件名"+ RandomUtil.randomString(5), "UTF-8");

				// 设置响应文件类型PDF
				response.setContentType("application/pdf");
				response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");

				// 创建 PdfWriter 实例,将 PDF 内容写入 HTTP 响应流,1.7版本
				PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
				writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
				document.open();

				// 中文字体
				BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
				Font font = new Font(baseFont, 12);

				// 添加文档标题,居中,间距20,添加到文档中
				Paragraph pdfTitle = new Paragraph(title, font);
				pdfTitle.setAlignment(Element.ALIGN_CENTER);
				pdfTitle.setSpacingAfter(20f);
				document.add(pdfTitle);

				// 9 列的表格,宽度占满整个页面
				PdfPTable table = new PdfPTable(9);
				table.setWidthPercentage(100);

				// 单独设置前两2宽度
				float[] columnWidths = new float[9];
				columnWidths[0] = 3f;
				columnWidths[1] = 2f;
				for (int i = 2; i < 9; i++) {
					columnWidths[i] = 1f;
				}
				table.setTotalWidth(columnWidths);

				// 添加带斜线的单元格
				PdfPCell splitCell = new PdfPCell();
				// 绑定斜线事件
				splitCell.setCellEvent(new DiagonalLineEvent());
				// 设置单元格高度
				splitCell.setFixedHeight(30f);

				Paragraph day = new Paragraph("日", font);
				day.setAlignment(Element.ALIGN_RIGHT);
				splitCell.addElement(day);

				Paragraph month = new Paragraph("周", font);
				month.setAlignment(Element.ALIGN_LEFT);
				month.setSpacingBefore(-15f);
				splitCell.addElement(month);

				table.addCell(splitCell);
				// 添加第二列表头"周打卡数"
				PdfPCell cell2 = new PdfPCell(new Phrase("周打卡数", font));
				cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
				table.addCell(cell2);

				for (int i = 1; i <= 7; i++) {
					PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(i), font));
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					table.addCell(cell);
				}

				for (DataVo vo : vos) {
					PdfPCell monthCell = new PdfPCell(new Phrase(vo.getWeek(), font));
					monthCell.setFixedHeight(20f);
					monthCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					monthCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					table.addCell(monthCell);

					PdfPCell totalCell = new PdfPCell(new Phrase(String.valueOf(vo.getCount()), font));
					totalCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					totalCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					table.addCell(totalCell);

					List<Integer> days = vo.getDays();
					for (int i = 1; i <= 7; i++) {
						PdfPCell cell = null;
						if (days.contains(i)) {
							cell = new PdfPCell(new Phrase("√", font));
						} else {
							cell = new PdfPCell(new Phrase("", font));
						}
						cell.setHorizontalAlignment(Element.ALIGN_CENTER);
						cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
						table.addCell(cell);
					}
				}
				document.add(table);
			} catch (Exception e) {
				log.error("导出pdf文件"+e);
			} finally {
				if (document.isOpen()) {
					document.close();
				}
			}
		}
	}

3、测试结果

调用接口测试,响应pdf文件打开如下:

相关推荐
come112347 分钟前
深入Spring Boot的核心——配置管理(指南四)
java·spring boot·后端
come1123431 分钟前
深入分析JAR和WAR包的区别 (指南七)
android·spring boot·后端
武昌库里写JAVA1 小时前
Java 设计模式在 Spring 框架中的实践:工厂模式与单例模式
java·vue.js·spring boot·sql·学习
千里码aicood1 小时前
springboot+vue心理健康服务小程序(源码+文档+调试+基础修改+答疑)
数据库·vue.js·spring boot
李慕婉学姐1 小时前
【开题答辩过程】以《基于SpringBoot+Vue的扶贫助农平台的设计与实现》为例,不会开题答辩的可以进来看看
vue.js·spring boot·后端
麦兜*1 小时前
Redis高可用架构设计:主从复制、哨兵、Cluster集群模式深度对比
java·数据库·spring boot·redis·spring·spring cloud·缓存
王嘉俊9251 小时前
Redis 入门:高效缓存与数据存储的利器
java·数据库·redis·后端·spring·缓存·springboot
kangaroo.1 小时前
基于EasyExcel、FastExcel封装spring boot starter
spring boot·easyexcel·fastexcel
王维1 小时前
【shardingsphere-jdbc】分表实践
java·数据库
啦啦9117141 小时前
Print Conductor打印软件安装教程!一款非常好用的批量打印软件!支持PDF、Word、Excel、图片等
pdf·excel