> 文章列表 > springboot 整合itext 实现pdf下载

springboot 整合itext 实现pdf下载

springboot 整合itext 实现pdf下载

一 itext介绍

1.1 核心组件介绍

  Text中用文本块(Chunk)、短语(Phrase)和段落(paragraph)处理文本。
 1.文本块(Chunk)是处理文本的最小单位,有一串带格式(包括字体、颜色、大小)的字符串组成。如以下代码就是产生一个字体为HELVETICA、大小为10、带下划线的字符串:
 Chunk chunk1 = new Chunk(“This text is underlined”,FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));

 2.短语(Phrase)由一个或多个文本块(Chunk)组成,短语(Phrase)也可以设定字体,但对于其中以设定过字体的文本块(Chunk)无效。
  通过短语(Phrase)成员函数add可以将一个文本块(Chunk)加到短语(Phrase)中,如:phrase6.add(chunk);

 3.段落(paragraph)由一个或多个文本块(Chunk)或短语(Phrase)组成,相当于WORD文档中的段落概念,同样可以设定段落的字体大小、颜色等属性。
 另外也可以设定段落的首行缩进、对齐方式(左对齐、右对齐、居中对齐)。通过函数setAlignment可以设定段落的对齐方式,s
  etAlignment的参数1为居中对齐、2为右对齐、3为左对齐,默认为左对齐。
 4.Itext中处理表格在有PDFTable,Table。对于简单在表格处理可以用Table,但如果要处理复杂的表格就需要PDFTable进行处理.
 创建表格时,必须要指定列,行则不是必须的。
  建立表格之后,可以设定表格的属性,如:边框宽度、边框颜色、衬距(padding space 即单元

格之间的间距)大小等属性。

1.2  所需要字体ttf文件见资源上传库

 1.3 pom依赖设置

<!--pdf-->
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.9</version>
</dependency>

二  案例实操

2.1 案例1

1.代码

package com.ljf.pdf.util;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.List.*;/*** @ClassName: PdfTest* @Description: TODO* @Author: admin* @Date: 2023/04/17 18:55:14 * @Version: V1.0**/
public class PdfTest {public static char checked='\\u00FE';//    //http://www.alanwood.net/demos/wingdings.htmlpublic static char unchecked='\\u00A8';public static void main(String[] args) {try {createAllPdf();} catch (Exception e) {e.printStackTrace();}}/*** 抠模板** @throws Exception*/public static void createAllPdf() throws Exception {//1.设置字体编码BaseFont windings = BaseFont.createFont("d:/Wingdings.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font f12 = new Font(windings, 12f);Chunk check12=new Chunk((char)0x00FE,f12);Chunk uncheck12=new Chunk((char)0x00A8,f12);//复选框  //http://www.alanwood.net/demos/wingdings.htmlChunk radioChecked=new Chunk((char)0x00A4,f12);Chunk radioUnchecked=new Chunk((char)0x00A1,f12);//单选  //http://www.alanwood.net/demos/wingdings.htmlBaseFont simFangFont=BaseFont.createFont("d:/simfang.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fangNormal12=new Font(simFangFont,16f,Font.NORMAL);// 第一步,实例化一个document对象Document document = new Document();// 第二步,设置要到出的路径FileOutputStream out = new FileOutputStream("D:/workbook111.pdf");//如果是浏览器通过request请求需要在浏览器中输出则使用下面方式//OutputStream out = response.getOutputStream();// 第三步,设置字符BaseFont bfChinese = BaseFont.createFont("d:/SimHei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font fontZH = new Font(bfChinese, 12.0F, 0);Font fontChinese1 = new Font(bfChinese, 16, Font.BOLD);// 第四步,将pdf文件输出到磁盘PdfWriter writer = PdfWriter.getInstance(document, out);// 第五步,打开生成的pdf文件document.open();// 第六步,设置内容String title = "上汽通用汽车员工租车申请表";//document.add(new Paragraph(new Chunk("Logo", fontZH)));//document.add(new Paragraph(new Chunk(title, fontZH).setLocalDestination(title)));PdfPTable titleTable = new PdfPTable(5);titleTable.setWidthPercentage(100.0F);//占1列PdfPCell logoCell = new PdfPCell(new Phrase("LOGO", fontZH));logoCell.setColspan(1);logoCell.setBorderWidthTop(0);//取消边框展示logoCell.setBorderWidthRight(0);logoCell.setBorderWidthBottom(0);logoCell.setBorderWidthLeft(0);//ClassPathResource classPathResource = new ClassPathResource("C:/Users/Anear/IdeaProjects/tutor-system/tutor-teacher-service/src/main/resources/logo/logo.png");Image image = Image.getInstance("d:/11.jpg");logoCell.setImage(image);//展示图片logologoCell.setBorderWidth(120);logoCell.setFixedHeight(40);titleTable.addCell(logoCell);//占3列PdfPCell titleCell = new PdfPCell(new Phrase(title, fontChinese1));titleCell.setColspan(3);titleCell.setBorderWidthTop(0);titleCell.setBorderWidthRight(0);titleCell.setBorderWidthBottom(0);titleCell.setBorderWidthLeft(0);titleCell.setVerticalAlignment(Element.ALIGN_MIDDLE);//水平垂直居中titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);titleTable.addCell(titleCell);document.add(titleTable);//占1列PdfPCell kongCell = new PdfPCell(new Phrase("", fontChinese1));kongCell.setBorderWidthTop(0);kongCell.setBorderWidthRight(0);kongCell.setBorderWidthBottom(0);kongCell.setBorderWidthLeft(0);titleTable.addCell(kongCell);document.add(titleTable);document.add(new Paragraph(new Chunk("申请日期:2021-06-01", fontZH)));document.add(new Paragraph("\\n"));// 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列PdfPTable table = new PdfPTable(6);table.setWidthPercentage(100.0F);table.setHeaderRows(1);//1行标题table.getDefaultCell().setHorizontalAlignment(1);//标题占6列PdfPCell cell = new PdfPCell(new Phrase("个人信息", fontZH));cell.setColspan(6);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell);//12列,分两行table.addCell(new Paragraph("员工姓名", fontZH));table.addCell(new Paragraph("张亮Zhang Liang", fontZH));table.addCell(new Paragraph("工号", fontZH));table.addCell(new Paragraph("2849", fontZH));table.addCell(new Paragraph("工作部门", fontZH));table.addCell(new Paragraph("上汽通用JQSP", fontZH));table.addCell(new Paragraph("身份证号", fontZH));table.addCell(new Paragraph("320322199711125310", fontZH));table.addCell(new Paragraph("联系电话", fontZH));table.addCell(new Paragraph("17601251600", fontZH));table.addCell(new Paragraph("单位联系电话", fontZH));table.addCell(new Paragraph("18994131575", fontZH));//合并单元格,占6列PdfPCell cell2 = new PdfPCell(new Phrase("车辆需求信息", fontZH));cell2.setColspan(6);cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);cell2.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell2);table.addCell(new Paragraph("申购车型", fontZH));table.addCell(new Paragraph("全新一代别克君威豪华型", fontZH));table.addCell(new Paragraph("颜色", fontZH));table.addCell(new Paragraph("天空蓝", fontZH));table.addCell(new Paragraph("价格(指导价)", fontZH));table.addCell(new Paragraph("888888", fontZH));//合并单元格,占6列PdfPCell cell3 = new PdfPCell(new Phrase("审核情况", fontZH));cell3.setColspan(6);cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);cell3.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell3);//6列 ,占1table.addCell(new Paragraph("最终购车价格", fontZH));//6列,占3PdfPCell cell4 = new PdfPCell(new Phrase("88888", fontZH));cell4.setColspan(3);table.addCell(cell4);//6列 ,占1table.addCell(new Paragraph("部门审核盖章", fontZH));//6列 ,占1table.addCell(new Paragraph("", fontZH));//合并单元格,占6列PdfPCell cell5 = new PdfPCell(new Phrase("开票信息", fontZH));cell5.setColspan(6);cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);cell5.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell5);//6列 ,占1table.addCell(new Paragraph("与员工关系", fontZH));//6列 ,占5PdfPCell cell6 = new PdfPCell(new Phrase("本人", fontZH));cell6.setColspan(5);table.addCell(cell6);//6列 ,占1table.addCell(new Paragraph("开票人姓名", fontZH));//6列 ,占5PdfPCell cell7 = new PdfPCell(new Phrase("张亮", fontZH));cell7.setColspan(5);table.addCell(cell7);//6列 ,占1table.addCell(new Paragraph("身份证号", fontZH));//6列 ,占5PdfPCell cell8 = new PdfPCell(new Phrase("320322199711125310", fontZH));cell8.setColspan(5);table.addCell(cell8);//自定义Paragraph zdf= new Paragraph("复合意见", fontZH);//占1列table.addCell(zdf);Phrase  itemTitle=new Phrase();//单选itemTitle.add(radioChecked);itemTitle.add(new Chunk("同意",fangNormal12));itemTitle.add(new Chunk("      ",fangNormal12));itemTitle.add(radioUnchecked);itemTitle.add(new Chunk("不同意",fangNormal12));itemTitle.add(new Chunk("\\n",fangNormal12));//新的一行,复选框itemTitle.add(check12);itemTitle.add(new Chunk("已有数据",fangNormal12));//2个chuck 分割一段距离itemTitle.add(new Chunk("      ",fangNormal12));itemTitle.add(uncheck12);itemTitle.add(new Chunk("需要采集数据",fangNormal12));//占 5列PdfPCell zdyCell=createCell(itemTitle);zdyCell.setColspan(5);table.addCell(zdyCell);document.add(table);document.add(new Paragraph(String.valueOf(radioChecked),fangNormal12));document.add(new Paragraph(String.valueOf(radioUnchecked),fangNormal12));document.add(new Paragraph(new Chunk("( 注:本身清单自申请日的当季内有效)", fontZH).setLocalDestination(title)));document.add(new Paragraph("\\n"));// 第七步,关闭documentdocument.close();System.out.println(out);System.out.println("导出pdf成功~");}public static PdfPCell createCell(Phrase phrase){PdfPCell cell=new PdfPCell(phrase);cell.setVerticalAlignment(Element.ALIGN_CENTER);cell.setMinimumHeight(27f);return cell;
}public static void createAllPdf2() throws Exception {  // 第一步,实例化一个document对象BaseFont simFangFont=BaseFont.createFont("d:/simfang.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fangNormal12=new Font(simFangFont,12,Font.NORMAL);Document document = new Document();// 第二步,设置要到出的路径FileOutputStream out = new FileOutputStream("D:/workbook111.pdf");//如果是浏览器通过request请求需要在浏览器中输出则使用下面方式//OutputStream out = response.getOutputStream();// 第三步,设置字符BaseFont bfChinese = BaseFont.createFont("d:/SimHei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font fontZH = new Font(bfChinese, 12.0F, 0);Font fontChinese1 = new Font(bfChinese, 16, Font.BOLD);// 第四步,将pdf文件输出到磁盘PdfWriter writer = PdfWriter.getInstance(document, out);// 第五步,打开生成的pdf文件document.open();// 第六步,设置内容String title = "上汽通用汽车员工租车申请表";//document.add(new Paragraph(new Chunk("Logo", fontZH)));//document.add(new Paragraph(new Chunk(title, fontZH).setLocalDestination(title)));PdfPTable titleTable = new PdfPTable(5);titleTable.setWidthPercentage(100.0F);PdfPCell logoCell = new PdfPCell(new Phrase("LOGO", fontZH));logoCell.setColspan(1);logoCell.setBorderWidthTop(0);//取消边框展示logoCell.setBorderWidthRight(0);logoCell.setBorderWidthBottom(0);logoCell.setBorderWidthLeft(0);//ClassPathResource classPathResource = new ClassPathResource("C:/Users/Anear/IdeaProjects/tutor-system/tutor-teacher-service/src/main/resources/logo/logo.png");Image image = Image.getInstance("d:/11.jpg");logoCell.setImage(image);//展示图片logologoCell.setBorderWidth(120);logoCell.setFixedHeight(40);titleTable.addCell(logoCell);PdfPCell titleCell = new PdfPCell(new Phrase(title, fontChinese1));titleCell.setColspan(3);titleCell.setBorderWidthTop(0);titleCell.setBorderWidthRight(0);titleCell.setBorderWidthBottom(0);titleCell.setBorderWidthLeft(0);titleCell.setVerticalAlignment(Element.ALIGN_MIDDLE);//水平垂直居中titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);titleTable.addCell(titleCell);document.add(titleTable);PdfPCell kongCell = new PdfPCell(new Phrase("", fontChinese1));kongCell.setBorderWidthTop(0);kongCell.setBorderWidthRight(0);kongCell.setBorderWidthBottom(0);kongCell.setBorderWidthLeft(0);titleTable.addCell(kongCell);document.add(titleTable);document.add(new Paragraph(new Chunk("申请日期:2021-06-01", fontZH)));document.add(new Paragraph("\\n"));// 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列PdfPTable table = new PdfPTable(6);table.setWidthPercentage(100.0F);table.setHeaderRows(1);table.getDefaultCell().setHorizontalAlignment(1);PdfPCell cell = new PdfPCell(new Phrase("个人信息", fontZH));cell.setColspan(6);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell);table.addCell(new Paragraph("员工姓名", fontZH));table.addCell(new Paragraph("张亮Zhang Liang", fontZH));table.addCell(new Paragraph("工号", fontZH));table.addCell(new Paragraph("2849", fontZH));table.addCell(new Paragraph("工作部门", fontZH));table.addCell(new Paragraph("上汽通用JQSP", fontZH));table.addCell(new Paragraph("身份证号", fontZH));table.addCell(new Paragraph("320322199711125310", fontZH));table.addCell(new Paragraph("联系电话", fontZH));table.addCell(new Paragraph("17601251600", fontZH));table.addCell(new Paragraph("单位联系电话", fontZH));table.addCell(new Paragraph("18994131575", fontZH));PdfPCell cell2 = new PdfPCell(new Phrase("车辆需求信息", fontZH));cell2.setColspan(6);cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);cell2.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell2);table.addCell(new Paragraph("申购车型", fontZH));table.addCell(new Paragraph("全新一代别克君威豪华型", fontZH));table.addCell(new Paragraph("颜色", fontZH));table.addCell(new Paragraph("天空蓝", fontZH));table.addCell(new Paragraph("价格(指导价)", fontZH));table.addCell(new Paragraph("888888", fontZH));PdfPCell cell3 = new PdfPCell(new Phrase("审核情况", fontZH));cell3.setColspan(6);cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);cell3.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell3);table.addCell(new Paragraph("最终购车价格", fontZH));PdfPCell cell4 = new PdfPCell(new Phrase("88888", fontZH));cell4.setColspan(3);table.addCell(cell4);table.addCell(new Paragraph("部门审核盖章", fontZH));table.addCell(new Paragraph("", fontZH));PdfPCell cell5 = new PdfPCell(new Phrase("开票信息", fontZH));cell5.setColspan(6);cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);cell5.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell5);table.addCell(new Paragraph("与员工关系", fontZH));PdfPCell cell6 = new PdfPCell(new Phrase("本人", fontZH));cell6.setColspan(5);table.addCell(cell6);table.addCell(new Paragraph("开票人姓名", fontZH));PdfPCell cell7 = new PdfPCell(new Phrase("张亮", fontZH));cell7.setColspan(5);table.addCell(cell7);table.addCell(new Paragraph("身份证号", fontZH));PdfPCell cell8 = new PdfPCell(new Phrase("320322199711125310", fontZH));cell8.setColspan(5);table.addCell(cell8);//自定义String  type="1";List<String> dataList=new ArrayList<>();dataList.add("1");dataList.add("2");Paragraph itemTitle=new Paragraph();itemTitle= new Paragraph("身份证号2222", fontZH);table.addCell(itemTitle);/**PdfPCell cell9 = new PdfPCell(new Phrase("11111111111", fontZH));cell9.setColspan(5);table.addCell(cell9);**/// itemTitle.add(new Chunk("需求类别:",fangNormal12));if(dataList.contains("1")){itemTitle.add(new Chunk("采集需求:",fontZH));}else if(dataList.contains("2")){itemTitle.add(new Chunk("使用需求:",fontZH));}else{Chunk unCheck12=new Chunk((char)0x00A8);itemTitle.add(unCheck12);itemTitle.add(new Chunk("采集需求:",fontZH));itemTitle.add(unCheck12);itemTitle.add(new Chunk("使用需求:",fontZH));}PdfPCell cell9 = new PdfPCell(itemTitle);//cell9.setColspan(5);table.addCell(cell9);// PdfPCell mb=createCell(itemTitle);//mb.setColspan(8);//table.addCell(mb);document.add(table);document.add(new Paragraph(new Chunk("( 注:本身清单自申请日的当季内有效)", fontZH).setLocalDestination(title)));document.add(new Paragraph("\\n"));// 第七步,关闭documentdocument.close();System.out.println(out);System.out.println("导出pdf成功~");}}

2.效果

2.2 案例2

1.代码

public class TestTablePdf {public static void main(String[] args) throws IOException, DocumentException {createTablePdf();}public static void  createTablePdf() throws DocumentException, IOException {//1.设置字体编码BaseFont windings = BaseFont.createFont("d:/Wingdings.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//1.1 设置字体Font f12 = new Font(windings, 12f);BaseFont simFangFont=BaseFont.createFont("d:/simfang.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fangNormal12=new Font(simFangFont,16f,Font.NORMAL);//1.2 设置复选框Chunk check12=new Chunk((char)0x00FE,f12);Chunk uncheck12=new Chunk((char)0x00A8,f12);//复选框  //http://www.alanwood.net/demos/wingdings.html//设置下拉框Chunk radioChecked=new Chunk((char)0x00A4,f12);Chunk radioUnchecked=new Chunk((char)0x00A1,f12);//单选  //http://www.alanwood.net/demos/wingdings.html// 第一步,实例化一个document对象Document document = new Document();// 第二步,设置要到出的路径FileOutputStream out = new FileOutputStream("D:/workorder111.pdf");//如果是浏览器通过request请求需要在浏览器中输出则使用下面方式//OutputStream out = response.getOutputStream();// 第三步,设置字符BaseFont bfChinese = BaseFont.createFont("d:/SimHei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font fontZH = new Font(bfChinese, 12.0F, 0);Font fontChinese1 = new Font(bfChinese, 16, Font.BOLD);// 第四步,将pdf文件输出到磁盘PdfWriter writer = PdfWriter.getInstance(document, out);// 第五步,打开生成的pdf文件document.open();PdfPTable table = new PdfPTable(4);table.setWidthPercentage(100.0F);table.setHeaderRows(1);//1行标题table.getDefaultCell().setHorizontalAlignment(1);//标题占4列PdfPCell cell = new PdfPCell(new Phrase("数据质量问题申请单", fontZH));cell.setColspan(4);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell);//=======================================第2行//4列,占1table.addCell(new Paragraph("产品名称", fontZH));//4列,占1PdfPCell cell22 = new PdfPCell(new Phrase("主办券商联系人", fontZH));cell22.setColspan(1);table.addCell(cell22);//4列,占1table.addCell(new Paragraph("产品类型", fontZH));//4列,占1PdfPCell cell24 = new PdfPCell(new Phrase("api", fontZH));cell24.setColspan(1);table.addCell(cell24);//=======================================第3行PdfPCell cell31 = new PdfPCell(new Phrase("问题描述", fontZH));cell31.setColspan(1);table.addCell(cell31);Phrase hhRow=new Phrase();hhRow.add(new Chunk("api不对",fontZH));hhRow.add(new Chunk("\\n",fangNormal12));hhRow.add(new Chunk("\\n",fangNormal12));PdfPCell cell32 = createCell(hhRow);cell32.setColspan(3);table.addCell(cell32);//=======================================第4行//合并单元格,占6列PdfPCell cell4= new PdfPCell(new Phrase("数据问题复核意见", fontZH));cell4.setColspan(4);cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);cell4.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(cell4);//=======================================第5行Paragraph zdf= new Paragraph("复合意见", fontZH);//占1列table.addCell(zdf);Phrase  itemTitle=new Phrase();//单选itemTitle.add(radioChecked);itemTitle.add(new Chunk("同意",fangNormal12));itemTitle.add(new Chunk("      ",fangNormal12));itemTitle.add(radioUnchecked);itemTitle.add(new Chunk("不同意",fangNormal12));itemTitle.add(new Chunk("\\n",fangNormal12));//新的一行,复选框itemTitle.add(check12);itemTitle.add(new Chunk("已有数据",fangNormal12));//2个chuck 分割一段距离itemTitle.add(new Chunk("      ",fangNormal12));itemTitle.add(uncheck12);itemTitle.add(new Chunk("需要采集数据",fangNormal12));//占 5列PdfPCell zdyCell=createCell(itemTitle);zdyCell.setColspan(5);table.addCell(zdyCell);document.add(table);document.add(new Paragraph(new Chunk("申请日期:2021-06-01", fontZH)));// 第七步,关闭documentdocument.close();System.out.println(out);System.out.println("导出pdf成功~");}public static PdfPCell createCell(Phrase phrase){PdfPCell cell=new PdfPCell(phrase);cell.setVerticalAlignment(Element.ALIGN_CENTER);cell.setMinimumHeight(27f);return cell;}
}

2.效果