赞
踩
我们经常会遇到要导出pdf的需求,方式有很多种 今天的教程是采用itext的方式生成pdf
先来看一下效果
OK,下面开始教程
1.准备工作-下载相关依赖
- <!--itext相关-->
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>kernel</artifactId>
- <version>7.0.3</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>io</artifactId>
- <version>7.0.3</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>layout</artifactId>
- <version>7.0.3</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>font-asian</artifactId>
- <version>7.0.3</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>pdfa</artifactId>
- <version>7.0.3</version>
- </dependency>

2.创建实体对象
- package com.example.demo.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @Author: Mr.Z
- * @Date: 2024年03月22日 14:53
- **/
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class ChildInfo {
- /**
- * 幼儿姓名
- */
- private String stuName;
- /**
- * 性别
- */
- private String sex;
- /**
- * 是否独生子女
- */
- private String onlyChild;
- /**
- * 血型
- */
- private String blood;
- /**
- * 幼儿身份证号
- */
- private String stuIdCard;
- /**
- * 幼儿出生日期
- */
- private String stuDate;
- /**
- * 民族
- */
- private String nation;
- /**
- * 幼儿照片
- */
- private String childPic;
- /**
- * 幼儿籍贯
- *
- */
- private String nativePlace;
- /**
- * 出生所在地
- */
- private String birthPlace;
- /**
- * 幼儿户籍地区码
- */
- private String nativeCode;
- }

3.编写pdf样式及内容,这里相关介绍我都写到注释里面了,很详细
- package com.example.demo.service.impl;
-
- import com.example.demo.entity.ChildInfo;
- import com.itextpdf.io.image.ImageDataFactory;
- import com.itextpdf.kernel.color.Color;
- import com.itextpdf.kernel.color.DeviceRgb;
- import com.itextpdf.kernel.font.PdfFont;
- import com.itextpdf.kernel.font.PdfFontFactory;
- import com.itextpdf.kernel.pdf.PdfDocument;
- import com.itextpdf.kernel.pdf.PdfWriter;
- import com.itextpdf.layout.Document;
- import com.itextpdf.layout.element.Cell;
- import com.itextpdf.layout.element.Image;
- import com.itextpdf.layout.element.Paragraph;
- import com.itextpdf.layout.element.Table;
- import com.itextpdf.layout.property.TextAlignment;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.UUID;
-
- /**
- * @Author: Mr.Z
- * @Date: 2024年03月22日 9:37
- **/
- @Service
- public class ItextPdfServiceImpl {
- /**
- * 下载到本地
- * @param stu
- * @throws IOException
- */
- public void downLoad(ChildInfo stu) throws IOException {
- //获取项目根路径
- String baseUrl=System.getProperty("user.dir")+"/";
- //随机命名
- String templateUUId = UUID.randomUUID().toString();
- //生成的pdf路径+名称
- String pdf = baseUrl+templateUUId+".pdf";
-
- //1、创建流对象
- PdfWriter pdfWriter=new PdfWriter(new File(pdf));
- //2、创建文档对象
- PdfDocument pdfDocument=new PdfDocument(pdfWriter);
- //3、创建内容文档对象
- Document document=new Document(pdfDocument);
-
- //创建内容
- Paragraph paragraph=new Paragraph("报名信息");
- //设置字体,解决中文显示问题
- PdfFont font= PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H",true);
- paragraph.setFont(font);
- paragraph.setTextAlignment(TextAlignment.CENTER);//居中
- document.add(paragraph);
-
- //设置表格每列宽度
- Table table=new Table(new float[]{108,65,35,50,90,70,40,40,80});
- //设置表格宽度百分比
- table.setWidthPercent(100);
- //创建表头
- Cell head=new Cell(1,9); //一行9列
- //创建标题
- Color customColor = new DeviceRgb(255, 165, 0); //这是一个橙色示例,您可以替换为其他RGB值
- head.add(new Paragraph("幼儿身份信息"))
- .setFont(font) // 设置字体
- .setTextAlignment(TextAlignment.LEFT) // 居左
- .setBackgroundColor(customColor); // 设置自定义背景颜色
-
- Cell cell1=new Cell().add(new Paragraph("姓名").setFont(font));
- Cell cell2=new Cell().add(new Paragraph(stu.getStuName()).setFont(font));
- Cell cell3=new Cell().add(new Paragraph("性别").setFont(font));
- Cell cell4=new Cell().add(new Paragraph(stu.getSex()).setFont(font));
- Cell cell5=new Cell().add(new Paragraph("是否独生子女").setFont(font));
- Cell cell6=new Cell().add(new Paragraph(stu.getOnlyChild()).setFont(font));
- Cell cell7=new Cell().add(new Paragraph("血型").setFont(font));
- Cell cell8=new Cell().add(new Paragraph(stu.getBlood()).setFont(font));
-
- table.addCell(cell1);
- table.addCell(cell2);
- table.addCell(cell3);
- table.addCell(cell4);
- table.addCell(cell5);
- table.addCell(cell6);
- table.addCell(cell7);
- table.addCell(cell8);
-
- //加入表格
- table.addHeaderCell(head);
- table.addHeaderCell(new Cell(1,9));//一行9列
- //加入图片
- String picUrl = stu.getChildPic();
- Image image=new Image(ImageDataFactory.create(picUrl));
- //设置图片缩放比例 水平 垂直
- //image.scale(0.5f,0.5f);
- float scaledWidth =60; // 设置宽度(毫米)
- float scaledHeight = 150; // 设置高度(毫米)
- image.scaleToFit(scaledWidth, scaledHeight);
- /**
- * 这里解释一下new Cell(3,1)的意思 相当于垂直合并一列的3行单元格
- * 其他单元格的合并都是如此进行调试
- */
- //将图片插入表格
- Cell cell9 = new Cell(3,1);
- cell9.add(image);
- table.addCell(cell9);
-
- table.addCell(new Cell().add(new Paragraph("身份证号码").setFont(font)));
- table.addCell(new Cell(1,3).add(new Paragraph(stu.getStuIdCard()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿出生日期").setFont(font)));
- table.addCell(new Cell().add(new Paragraph(stu.getStuDate()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("民族").setFont(font)));
- table.addCell(new Cell().add(new Paragraph(stu.getNation()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿籍贯").setFont(font)));
- table.addCell(new Cell(1,7).add(new Paragraph(stu.getNativePlace()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("出生所在地").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph(stu.getBirthPlace()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿户籍地区码").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph(stu.getNativeCode()).setFont(font)));
-
- Cell head2=new Cell(1,9); //一行9列
- //创建标题
- Color customColor2 = new DeviceRgb(0,255,0); //这是一个绿色示例,您可以替换为其他RGB值
- head2.add(new Paragraph("家长信息"))
- .setFont(font) // 设置字体
- .setTextAlignment(TextAlignment.LEFT) // 居左
- .setBackgroundColor(customColor2); // 设置自定义背景颜色
- table.addCell(head2);
- table.addCell(new Cell().add(new Paragraph("父母信息").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph("爸爸胡英俊,妈妈张小丽").setFont(font)));
-
- //输出表格
- document.add(table);
- document.close();
- System.out.println("pdf生成完成!");
- }
-
- /**
- * 生成流返回给浏览器
- */
- public void downLoadStream(ChildInfo stu, HttpServletResponse response) throws Exception {
-
- OutputStream os = response.getOutputStream();
- //1、创建流对象
- PdfWriter pdfWriter=new PdfWriter(os);
- //2、创建文档对象
- PdfDocument pdfDocument=new PdfDocument(pdfWriter);
- //3、创建内容文档对象
- Document document=new Document(pdfDocument);
-
- //创建内容
- Paragraph paragraph=new Paragraph("报名信息");
- //设置字体,解决中文显示问题
- PdfFont font= PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H",true);
- paragraph.setFont(font);
- paragraph.setTextAlignment(TextAlignment.CENTER);//居中
- document.add(paragraph);
-
- //设置表格每列宽度
- Table table=new Table(new float[]{108,65,35,50,90,70,40,40,80});
- //设置表格宽度百分比
- table.setWidthPercent(100);
- //创建表头
- Cell head=new Cell(1,9); //一行9列
- //创建标题
- Color customColor = new DeviceRgb(255, 165, 0); //这是一个橙色示例,您可以替换为其他RGB值
- head.add(new Paragraph("幼儿身份信息"))
- .setFont(font) // 设置字体
- .setTextAlignment(TextAlignment.LEFT) // 居左
- .setBackgroundColor(customColor); // 设置自定义背景颜色
-
- Cell cell1=new Cell().add(new Paragraph("姓名").setFont(font));
- Cell cell2=new Cell().add(new Paragraph(stu.getStuName()).setFont(font));
- Cell cell3=new Cell().add(new Paragraph("性别").setFont(font));
- Cell cell4=new Cell().add(new Paragraph(stu.getSex()).setFont(font));
- Cell cell5=new Cell().add(new Paragraph("是否独生子女").setFont(font));
- Cell cell6=new Cell().add(new Paragraph(stu.getOnlyChild()).setFont(font));
- Cell cell7=new Cell().add(new Paragraph("血型").setFont(font));
- Cell cell8=new Cell().add(new Paragraph(stu.getBlood()).setFont(font));
-
- table.addCell(cell1);
- table.addCell(cell2);
- table.addCell(cell3);
- table.addCell(cell4);
- table.addCell(cell5);
- table.addCell(cell6);
- table.addCell(cell7);
- table.addCell(cell8);
-
- //加入表格
- table.addHeaderCell(head);
- table.addHeaderCell(new Cell(1,9));//一行9列
- //加入图片
- String picUrl = stu.getChildPic();
- Image image=new Image(ImageDataFactory.create(picUrl));
- //设置图片缩放比例 水平 垂直
- //image.scale(0.5f,0.5f);
- float scaledWidth =60; // 设置宽度(毫米)
- float scaledHeight = 150; // 设置高度(毫米)
- image.scaleToFit(scaledWidth, scaledHeight);
- /**
- * 这里解释一下new Cell(3,1)的意思 相当于垂直合并一列的3行单元格
- * 其他单元格的合并都是如此进行调试
- */
- //将图片插入表格
- Cell cell9 = new Cell(3,1);
- cell9.add(image);
- table.addCell(cell9);
-
- table.addCell(new Cell().add(new Paragraph("身份证号码").setFont(font)));
- table.addCell(new Cell(1,3).add(new Paragraph(stu.getStuIdCard()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿出生日期").setFont(font)));
- table.addCell(new Cell().add(new Paragraph(stu.getStuDate()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("民族").setFont(font)));
- table.addCell(new Cell().add(new Paragraph(stu.getNation()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿籍贯").setFont(font)));
- table.addCell(new Cell(1,7).add(new Paragraph(stu.getNativePlace()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("出生所在地").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph(stu.getBirthPlace()).setFont(font)));
- table.addCell(new Cell().add(new Paragraph("幼儿户籍地区码").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph(stu.getNativeCode()).setFont(font)));
-
- Cell head2=new Cell(1,9); //一行9列
- //创建标题
- Color customColor2 = new DeviceRgb(0,255,0); //这是一个绿色示例,您可以替换为其他RGB值
- head2.add(new Paragraph("家长信息"))
- .setFont(font) // 设置字体
- .setTextAlignment(TextAlignment.LEFT) // 居左
- .setBackgroundColor(customColor2); // 设置自定义背景颜色
- table.addCell(head2);
- table.addCell(new Cell().add(new Paragraph("父母信息").setFont(font)));
- table.addCell(new Cell(1,8).add(new Paragraph("爸爸胡英俊,妈妈张小丽").setFont(font)));
-
- //输出表格
- document.add(table);
-
- // 设置响应头信息
- response.setContentType("application/pdf");
- response.setHeader("Content-disposition", "attachment;filename="+new String(stu.getStuName().getBytes("gb2312"),"iso-8859-1") + ".pdf");
-
- // 关闭文档,此时PDF内容已写入到HttpServletResponse的OutputStream
- document.close();
- os.close();
-
- System.out.println("pdf生成完成!");
- }
- }
-

4.调用示例 controller调用
- package com.example.demo.itext;
-
- import com.example.demo.entity.ChildInfo;
- import com.example.demo.service.impl.ItextPdfServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * @Author: Mr.Z
- * @Date: 2024年03月22日 9:36
- **/
- @RestController
- @RequestMapping("/itext")
- public class ItextPdfController {
- @Autowired
- private ItextPdfServiceImpl itextPdfService;
-
- @GetMapping("/download")
- public void downLoad(HttpServletResponse response) throws Exception {
- ChildInfo childInfo = new ChildInfo();
- childInfo.setStuName("胡图图");
- childInfo.setSex("男");
- childInfo.setOnlyChild("是");
- childInfo.setBlood("O");
- childInfo.setStuIdCard("123456789987654321");
- childInfo.setStuDate("2018-05-20");
- childInfo.setNation("汉");
- childInfo.setNativePlace("翻斗花园");
- childInfo.setBirthPlace("翻斗人民医院");
- childInfo.setNativeCode("8899");
- childInfo.setChildPic("https://img2.baidu.com/it/u=294455873,3061678111&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1711213200&t=a29267da1ffde9810763fb027091cee0");
- itextPdfService.downLoadStream(childInfo,response);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。