SpringBoot集成OpenPDF导出pdf

1、引入依赖(最后一个支持java8的版本)

java 复制代码
<dependency>
     <groupId>com.github.librepdf</groupId>
     <artifactId>openpdf</artifactId>
     <version>1.3.34</version>
</dependency>
<dependency>
     <groupId>com.github.librepdf</groupId>
     <artifactId>openpdf-fonts-extra</artifactId>
     <version>1.3.34</version>
</dependency>

2、编码

java 复制代码
 String outputPath =  "d:\\pdf" + File.separator;
        File file = new File(outputPath);
        if (!file.exists()) {
            file.mkdir();
        }
        outputPath = outputPath + "hello.pdf";

        FileOutputStream fileOutputStream = new FileOutputStream(outputPath);

        // 设置字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font titleFont = new Font(bfChinese, 13, Font.BOLD, Color.BLACK);
        Font docFont = new Font(bfChinese, 10, Font.UNDEFINED, Color.BLACK);

        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, fileOutputStream);
        document.open();

        // 初始化一个4列的表格,超过自动换行
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100f);
        table.setSpacingAfter(20f);

        Paragraph paragraph = new Paragraph("检查表名称", titleFont);
        PdfPCell cell = new PdfPCell(paragraph);
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        cell.setColspan(4);
        table.addCell(cell);

        paragraph = new Paragraph("检查人", docFont);
        cell = new PdfPCell(paragraph);
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(taskResp.getOrgUserName(), docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查时间", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(TimeUtil.get_yyyyMMddHHmmss(taskResp.getCompleteTime()), docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);


        cell = new PdfPCell(new Paragraph("检查事项", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查内容", docFont));
        cell.setFixedHeight(20F);
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查意见", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

         //  循环处理数据,可删除
        for (InspectItemMoldResp inspectItemMoldResp : inspectItemResp.getMoldList()) {
            for (int i = 0; i < inspectItemMoldResp.getPatternList().size(); i++) {
                if (i == 0) {
                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getName(), docFont));
                    // cell.setFixedHeight(inspectItemMoldResp.getPatternList().size() * 20F);
                    cell.setRowspan(inspectItemMoldResp.getPatternList().size());
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置居中对齐
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setColspan(2);
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(
                            StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
                                    .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
                                    .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cell);

                } else {
                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setColspan(2);
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(
                            StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
                                    .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
                                    .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cell);
                }
            }
        }

        document.add(table);
        document.close();// 关闭文档
        fileOutputStream.flush();
        fileOutputStream.close();
相关推荐
Chrikk1 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*1 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue1 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man1 小时前
【go从零单排】go语言中的指针
开发语言·后端·golang
苹果醋33 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
Wx-bishekaifayuan3 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer083 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
Yaml44 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理