1. Introduce dependencies
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
2. Create a tool class
package com.csg.ats.util; import cn.hutool.core.date.DateUtil; import com.csg.ats.entity.ReportInfo; import com.csg.ats.enums.ProductTypeEnum; import com.csg.ats.enums.ReportTypeEnum; import com.csg.ats.enums.ResultEnum; import com.csg.ats.vo.ReportVo; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; public class DownloadPDFUtil { public static void downloadPdf(ReportVo reportVo, HttpServletResponse response) throws IOException, DocumentException { //Set the response format response.setContentType("application/pdf"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); //Set the specification to A4 paper Rectangle rect = new Rectangle(PageSize. A4); //Create document instance Document doc = new Document(rect); //Add Chinese font BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //Set the font style Font boldFont = new Font(bfChinese, 10, Font.NORMAL); Font overBoldFont = new Font(bfChinese, 10, Font.BOLD); Font redFont = new Font(bfChinese, 10, Font.NORMAL,CMYKColor.RED); Font greenFont = new Font(bfChinese, 10, Font.NORMAL,CMYKColor.GREEN); Font secondTitleFont = new Font(bfChinese, 18, Font.BOLD); //Set the font com.itextpdf.text.Font fontChinese15 = new com.itextpdf.text.Font(bfChinese, 15, com.itextpdf.text.Font.NORMAL); com.itextpdf.text.Font fontChinese14 = new com.itextpdf.text.Font(bfChinese, 14, com.itextpdf.text.Font.NORMAL); //Set pdf title String title = reportVo.getPileCode() + "Report Details"; response.setHeader("Content-disposition", "attachment; filename=".concat(String.valueOf(URLEncoder.encode(title + ".pdf", "UTF-8")))); OutputStream outputStream = response. getOutputStream(); PdfWriter. getInstance(doc, outputStream); doc. open(); doc. newPage(); //Create a new paragraph, use the second-level heading Paragraph p1 = new Paragraph(title, secondTitleFont); //Set the row height and center the title p1. setLeading(18); p1.setAlignment(Element.ALIGN_CENTER); //Add the paragraph to the document doc. add(p1); //Set an empty paragraph, the line height is 18, no content is displayed, typesetting needs Paragraph blankRow1 = new Paragraph(18f, " ", fontChinese14); doc.add(blankRow1); //set the width of the table int[] width1 = {70, 70}; //Create title Paragraph p2 = new Paragraph("Version information and nameplate scanning content", fontChinese14); p2.setAlignment(Element.ALIGN_CENTER); doc. add(p2); //For typesetting, set an empty paragraph Paragraph blankRow2 = new Paragraph(16f, " ", fontChinese14); doc.add(blankRow2); //Report overview information, table1, table2, table3 display //Create a new table with 2 columns PdfPTable table1 = new PdfPTable(2); table1.setWidths(width1); //In the first line, assign values to the cells, each cell is a paragraph, and the font of each paragraph is bold PdfPCell cell11 = new PdfPCell(new Paragraph("Pile Code: " + reportVo.getPileCode(), boldFont)); PdfPCell cell12 = new PdfPCell(new Paragraph("Report test time: " + DateUtil.format(reportVo.getStartTime(),"yyyy-MM-dd HH:mm:ss"), boldFont)); //Set the cell border to 0 cell11.setBorder(1); cell11.setPadding(5); cell11.setMinimumHeight(30); cell12.setBorder(1); cell12.setPadding(5); cell12.setMinimumHeight(30); table1. addCell(cell11); table1. addCell(cell12); doc. add(table1); PdfPTable table2 = new PdfPTable(2); table2.setWidths(width1); PdfPCell cell21 = new PdfPCell(new Paragraph("Tester: " + reportVo.getTester(), boldFont)); PdfPCell cell22; if (0 == reportVo. getResult()){ cell22 = new PdfPCell(new Paragraph("report result: " + ResultEnum.getEnum(reportVo.getResult()), greenFont)); } else { cell22 = new PdfPCell(new Paragraph("report result: " + ResultEnum.getEnum(reportVo.getResult()), redFont)); } //Set the cell border to 0 cell21.setBorder(1); cell21.setPadding(5); cell21.setMinimumHeight(30); cell22.setBorder(1); cell22.setPadding(5); cell22.setMinimumHeight(30); table2. addCell(cell21); table2. addCell(cell22); doc. add(table2); PdfPTable table3 = new PdfPTable(2); table3.setWidths(width1); PdfPCell cell31 = new PdfPCell(new Paragraph("Report Type: " + ReportTypeEnum.getEnum(reportVo.getReportType()), boldFont)); PdfPCell cell32 = new PdfPCell(new Paragraph("Product Type: " + ProductTypeEnum.getEnum(reportVo.getProductType()), boldFont)); //Set the cell border to 0 cell31.setBorder(1); cell31.setPadding(5); cell31.setMinimumHeight(30); cell32.setBorder(1); cell32.setPadding(5); cell32.setMinimumHeight(30); table3. addCell(cell31); table3. addCell(cell32); doc. add(table3); int[] width3 = {70}; PdfPTable table12 = new PdfPTable(1); table12.setWidths(width3); PdfPCell cell121 = new PdfPCell(new Paragraph("Item Type: " + reportVo.getFileType(), boldFont)); //Set the cell border to 0 cell121.setBorder(1); cell121.setPadding(5); cell121.setMinimumHeight(30); table12. addCell(cell121); doc. add(table12); //The version information is displayed by table4, table5, table6, table7, table10, table11 //Create a new table with 3 columns PdfPTable table4 = new PdfPTable(2); table4.setWidths(width1); PdfPCell cell41 = new PdfPCell(new Paragraph("PC version: " + reportVo.getUpVersion(), boldFont)); PdfPCell cell42 = new PdfPCell(new Paragraph("tooling software version: " + reportVo.getSoftwareVersion(), boldFont)); //Set the cell border to 0 cell41.setBorder(1); cell41.setPadding(5); cell41.setMinimumHeight(30); cell42.setBorder(1); cell42.setPadding(5); cell42.setMinimumHeight(30); table4. addCell(cell41); table4. addCell(cell42); doc. add(table4); PdfPTable table5 = new PdfPTable(2); table5.setWidths(width1); PdfPCell cell51 = new PdfPCell(new Paragraph("Equipment number: " + reportVo.getEquipmentCode(), boldFont)); PdfPCell cell52 = new PdfPCell(new Paragraph("Stub test program version: " + reportVo.getTestProgramVersion(), boldFont)); //Set the cell border to 0 cell51.setBorder(1); cell51.setPadding(5); cell51.setMinimumHeight(30); cell52.setBorder(1); cell52.setPadding(5); cell52.setMinimumHeight(30); table5. addCell(cell51); table5. addCell(cell52); doc. add(table5); PdfPTable table6 = new PdfPTable(2); table6.setWidths(width1); PdfPCell cell61 = new PdfPCell(new Paragraph("Pile test program time: " + DateUtil.format(reportVo.getTestProgramTime(),"yyyy-MM-dd HH:mm:ss"), boldFont)); PdfPCell cell62 = new PdfPCell(new Paragraph("CPU UUID: " + reportVo.getCpuUuid(), boldFont)); //Set the cell border to 0 cell61.setBorder(1); cell61.setPadding(5); cell61.setMinimumHeight(30); cell62.setBorder(1); cell62.setPadding(5); cell62.setMinimumHeight(30); table6. addCell(cell61); table6. addCell(cell62); doc. add(table6); PdfPTable table7 = new PdfPTable(2); table7.setWidths(width1); PdfPCell cell71 = new PdfPCell(new Paragraph("Pile boot version: " + reportVo.getBootVersion(), boldFont)); PdfPCell cell72 = new PdfPCell(new Paragraph("Pile hardware version: " + reportVo.getHardwareVersion(), boldFont)); //Set the cell border to 0 cell71.setBorder(1); cell71.setPadding(5); cell71.setMinimumHeight(30); cell72.setBorder(1); cell72.setPadding(5); cell72.setMinimumHeight(30); table7. addCell(cell71); table7. addCell(cell72); doc. add(table7); PdfPTable table10 = new PdfPTable(2); table10.setWidths(width1); PdfPCell cell101 = new PdfPCell(new Paragraph("BrushCard version: " + reportVo.getBrushCardVersion(), boldFont)); PdfPCell cell102 = new PdfPCell(new Paragraph("Pile screen version: " + reportVo.getScreenVersion(), boldFont)); //Set the cell border to 0 cell101.setBorder(1); cell101.setPadding(5); cell101.setMinimumHeight(30); cell102.setBorder(1); cell102.setPadding(5); cell102.setMinimumHeight(30); table10. addCell(cell101); table10. addCell(cell102); doc.add(table10); PdfPTable table11 = new PdfPTable(2); table11.setWidths(width1); PdfPCell cell111 = new PdfPCell(new Paragraph("Pile factory program version: " + reportVo.getFactoryVersion(), boldFont)); PdfPCell cell112 = new PdfPCell(new Paragraph("Pile factory program time: " + DateUtil.format(reportVo.getFactoryTime(),"yyyy-MM-dd HH:mm:ss"), boldFont)); //Set the cell border to 0 cell111.setBorder(1); cell111.setPadding(5); cell111.setMinimumHeight(30); cell112.setBorder(1); cell112.setPadding(5); cell112.setMinimumHeight(30); table11. addCell(cell111); table11. addCell(cell112); doc.add(table11); //nameplate information, table8, table9 display //Create a new table with 2 columns PdfPTable table8 = new PdfPTable(2); table8.setWidths(width1); PdfPCell cell81 = new PdfPCell(new Paragraph("BOM number: " + reportVo.getBomCode(), boldFont)); PdfPCell cell82 = new PdfPCell(new Paragraph("Production model: " + reportVo.getProductModel(), boldFont)); //Set the cell border to 0 cell81.setBorder(1); cell81.setPadding(5); cell81.setMinimumHeight(30); cell82.setBorder(1); cell82.setPadding(5); cell82.setMinimumHeight(30); table8. addCell(cell81); table8. addCell(cell82); doc. add(table8); PdfPTable table9 = new PdfPTable(2); table8.setWidths(width1); PdfPCell cell91 = new PdfPCell(new Paragraph("Production work order number: " + reportVo.getProductCode(), boldFont)); PdfPCell cell92 = new PdfPCell(new Paragraph("Production date: " + reportVo.getProductTime(), boldFont)); //Set the cell border to 0 cell91.setBorder(1); cell91.setPadding(5); cell91.setMinimumHeight(30); cell92.setBorder(1); cell92.setPadding(5); cell92.setMinimumHeight(30); table9. addCell(cell91); table9. addCell(cell92); doc. add(table9); // start another page doc. newPage(); //new paragraph Paragraph p3 = new Paragraph("Details of test content", fontChinese15); //set row height //Set the title to center p3.setAlignment(Element.ALIGN_CENTER); doc. add(p3); doc.add(blankRow2); // set the width int[] width2 = {20, 50, 50, 15}; //Approval Process PdfPTable table = new PdfPTable(4); table.setWidths(width2); PdfPCell c1 = new PdfPCell(new Paragraph("Test item", overBoldFont)); PdfPCell c2 = new PdfPCell(new Paragraph("Test requirements", overBoldFont)); PdfPCell c3 = new PdfPCell(new Paragraph("Test Details", overBoldFont)); PdfPCell c4 = new PdfPCell(new Paragraph("Result", overBoldFont)); c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); c2.setHorizontalAlignment(Element.ALIGN_CENTER); c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); c3.setHorizontalAlignment(Element.ALIGN_CENTER); c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); c4.setHorizontalAlignment(Element.ALIGN_CENTER); table. addCell(c1); table. addCell(c2); table. addCell(c3); table. addCell(c4); doc. add(table); List<ReportInfo> reportInfos = reportVo. getReportInfos(); for (ReportInfo reportInfo : reportInfos) { PdfPTable tablex = new PdfPTable(4); tablex.setWidths(width2); PdfPCell cell1 = new PdfPCell(new Paragraph(reportInfo.getTestItem(), boldFont)); PdfPCell cell2 = new PdfPCell(new Paragraph(reportInfo.getTestRequire().replace(",","\ \ "), boldFont)); PdfPCell cell3 = new PdfPCell(new Paragraph(reportInfo.getTestDetail().replace(",","\ \ "), boldFont)); PdfPCell cell4; if (0 == reportInfo. getTestResult()){ cell4 = new PdfPCell(new Paragraph(ResultEnum. getEnum(reportInfo. getTestResult()), greenFont)); } else { cell4 = new PdfPCell(new Paragraph(ResultEnum. getEnum(reportInfo. getTestResult()), redFont)); } cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); tablex. addCell(cell1); tablex. addCell(cell2); tablex. addCell(cell3); tablex. addCell(cell4); doc. add(tablex); } doc. close(); } }
reportVo is the file content of the exported pdf file
This method is to create multiple single-line tables in the pdf page, each table can display specified information, and the format can also be set separately. The format of the pdf file exported through the template has a high limit, but the export function can also be realized