springboot集成itextpdf插件实现导出pdf格式文件

1、引入依赖

        <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、创建工具类

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 {

        //设置响应格式
        response.setContentType("application/pdf");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate,post-check=0,pre-check=0");
        response.setHeader("Pragma", "public");
        //设置规格为A4纸
        Rectangle rect = new Rectangle(PageSize.A4);
        //创建文档实例
        Document doc = new Document(rect);
        //添加中文字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //设置字体样式
        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);
        //设置字体
        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);
        //设置pdf标题
        String title = reportVo.getPileCode() + "报告详情";
        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();
        //新建段落,使用二级标题
        Paragraph p1 = new Paragraph(title, secondTitleFont);
        //设置行高,标题居中
        p1.setLeading(18);
        p1.setAlignment(Element.ALIGN_CENTER);
        //将段落添加到文档上
        doc.add(p1);

        //设置一个空段落,行高18,不显示内容,排版需要
        Paragraph blankRow1 = new Paragraph(18f, " ", fontChinese14);
        doc.add(blankRow1);
        //给表格设置宽度
        int[] width1 = {70, 70};
        //创建标题
        Paragraph p2 = new Paragraph("版本信息及铭牌扫码内容", fontChinese14);
        p2.setAlignment(Element.ALIGN_CENTER);
        doc.add(p2);
        //为了排版,设置空段落
        Paragraph blankRow2 = new Paragraph(16f, " ", fontChinese14);
        doc.add(blankRow2);
        //报告总览信息,table1、table2、table3显示
        //新建表格,列数为2
        PdfPTable table1 = new PdfPTable(2);
        table1.setWidths(width1);
        //第一行,给单元格进行赋值,每个单元格为一个段落,每个段落字体加粗
        PdfPCell cell11 = new PdfPCell(new Paragraph("桩编码:" + reportVo.getPileCode(), boldFont));
        PdfPCell cell12 = new PdfPCell(new Paragraph("报告测试时间:" +
                DateUtil.format(reportVo.getStartTime(),"yyyy-MM-dd HH:mm:ss"), boldFont));
        //将单元格边框设置为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("测试人员:" + reportVo.getTester(), boldFont));
        PdfPCell cell22;
        if (0 == reportVo.getResult()){
            cell22 = new PdfPCell(new Paragraph("报告结果:" + ResultEnum.getEnum(reportVo.getResult()), greenFont));
        }else {
            cell22 = new PdfPCell(new Paragraph("报告结果:" + ResultEnum.getEnum(reportVo.getResult()), redFont));
        }
        //将单元格边框设置为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("报告类型:" + ReportTypeEnum.getEnum(reportVo.getReportType()), boldFont));
        PdfPCell cell32 = new PdfPCell(new Paragraph("产品类型:" + ProductTypeEnum.getEnum(reportVo.getProductType()), boldFont));
        //将单元格边框设置为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("项目类型:" + reportVo.getFileType(), boldFont));
        //将单元格边框设置为0
        cell121.setBorder(1);
        cell121.setPadding(5);
        cell121.setMinimumHeight(30);
        table12.addCell(cell121);
        doc.add(table12);
        //版本信息由 table4、table5、table6、table7、table10、table11显示
        //新建表格,列数为3
        PdfPTable table4 = new PdfPTable(2);
        table4.setWidths(width1);
        PdfPCell cell41 = new PdfPCell(new Paragraph("上位机版本:" + reportVo.getUpVersion(), boldFont));
        PdfPCell cell42 = new PdfPCell(new Paragraph("工装软件版本:" + reportVo.getSoftwareVersion(), boldFont));
        //将单元格边框设置为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("工装设备号:" + reportVo.getEquipmentCode(), boldFont));
        PdfPCell cell52 = new PdfPCell(new Paragraph("桩测试程序版本:" + reportVo.getTestProgramVersion(), boldFont));
        //将单元格边框设置为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("桩测试程序时间:" +
                DateUtil.format(reportVo.getTestProgramTime(),"yyyy-MM-dd HH:mm:ss"), boldFont));
        PdfPCell cell62 = new PdfPCell(new Paragraph("CPU UUID:" + reportVo.getCpuUuid(), boldFont));
        //将单元格边框设置为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("桩boot版本:" + reportVo.getBootVersion(), boldFont));
        PdfPCell cell72 = new PdfPCell(new Paragraph("桩硬件版本:" + reportVo.getHardwareVersion(), boldFont));
        //将单元格边框设置为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("桩刷卡板版本:" + reportVo.getBrushCardVersion(), boldFont));
        PdfPCell cell102 = new PdfPCell(new Paragraph("桩屏幕版本:" + reportVo.getScreenVersion(), boldFont));
        //将单元格边框设置为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("桩出厂程序版本:" + reportVo.getFactoryVersion(), boldFont));
        PdfPCell cell112 = new PdfPCell(new Paragraph("桩出厂程序时间:" +
                DateUtil.format(reportVo.getFactoryTime(),"yyyy-MM-dd HH:mm:ss"), boldFont));
        //将单元格边框设置为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);
        //铭牌信息,table8、table9显示
        //新建表格,列数为2
        PdfPTable table8 = new PdfPTable(2);
        table8.setWidths(width1);
        PdfPCell cell81 = new PdfPCell(new Paragraph("BOM编号:" + reportVo.getBomCode(), boldFont));
        PdfPCell cell82 = new PdfPCell(new Paragraph("生产型号:" + reportVo.getProductModel(), boldFont));
        //将单元格边框设置为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("生产工单号:" + reportVo.getProductCode(), boldFont));
        PdfPCell cell92 = new PdfPCell(new Paragraph("生产日期:" + reportVo.getProductTime(), boldFont));
        //将单元格边框设置为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);

        //另起一页
        doc.newPage();
        //新建段落
        Paragraph p3 = new Paragraph("测试内容详情", fontChinese15);
        //设置行高
        //设置标题居中
        p3.setAlignment(Element.ALIGN_CENTER);
        doc.add(p3);
        doc.add(blankRow2);
        //设置宽度
        int[] width2 = {20, 50, 50, 15};
        //审批流程
        PdfPTable table = new PdfPTable(4);
        table.setWidths(width2);
        PdfPCell c1 = new PdfPCell(new Paragraph("测试项", overBoldFont));
        PdfPCell c2 = new PdfPCell(new Paragraph("测试要求", overBoldFont));
        PdfPCell c3 = new PdfPCell(new Paragraph("测试详细信息", overBoldFont));
        PdfPCell c4 = new PdfPCell(new Paragraph("结果", 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(",","\n\n"), boldFont));
            PdfPCell cell3 = new PdfPCell(new Paragraph(reportInfo.getTestDetail().replace(",","\n\n"), 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 为导出pdf文件的文件内容

这种方法是在pdf页中创建多个单行表格,每个表格中可以显示指定信息,格式也可单独设置,通过模板导出的pdf文件格式限制较高,但也可实现导出功能

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot导出PDF文件可以使用第三方库,比如iText或Apache PDFBox。以下是使用iText进行PDF导出的示例代码: 首先,需要将iText库添加到项目的依赖中。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 接下来,创建一个用于导出PDF的控制器,在该控制器中定义一个处理请求的方法。在方法中使用iText库来生成PDF文件。 ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; @Controller public class PdfController { @GetMapping("/exportpdf") public void exportPdf(HttpServletResponse response) throws IOException, DocumentException { response.setContentType(MediaType.APPLICATION_PDF_VALUE); response.setHeader("Content-Disposition", "attachment; filename=example.pdf"); Document document = new Document(); OutputStream outputStream = response.getOutputStream(); PdfWriter.getInstance(document, outputStream); document.open(); document.add(new Paragraph("Hello, World!")); document.close(); outputStream.close(); } } ``` 在上述代码中,我们使用`@GetMapping`注解来处理GET请求,并指定了导出PDF的URL为`/exportpdf`。在`exportPdf`方法中,我们首先设置响应的内容类型为PDF,然后设置响应头部的Content-Disposition,指定文件名为example.pdf。 接下来,创建一个`Document`实例,并使用`PdfWriter`将文档写入输出流中。在文档中添加内容,这里我们添加了一个简单的段落"Hello, World!"。最后关闭文档和输出流。 当访问`/exportpdf`URL时,将会下载一个名为example.pdfPDF文件,其中包含"Hello, World!"的内容。 这只是一个简单的示例,你可以根据实际需求来生成更复杂的PDF文件。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值