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();
相关推荐
Estar.Lee5 分钟前
查手机号归属地免费API接口教程
android·网络·后端·网络协议·tcp/ip·oneapi
弗拉唐1 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
CodeCraft Studio1 小时前
【实用技能】使用 TX Text Control 创建带有嵌入式附件的 PDF 文档
pdf·asp.net·.net
2401_857610032 小时前
SpringBoot社团管理:安全与维护
spring boot·后端·安全
凌冰_2 小时前
IDEA2023 SpringBoot整合MyBatis(三)
spring boot·后端·mybatis
码农飞飞2 小时前
深入理解Rust的模式匹配
开发语言·后端·rust·模式匹配·解构·结构体和枚举
一个小坑货2 小时前
Rust 的简介
开发语言·后端·rust
monkey_meng3 小时前
【遵守孤儿规则的External trait pattern】
开发语言·后端·rust
天天进步20153 小时前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket