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();
相关推荐
嗝屁小孩纸3 分钟前
企业级-生成PDF移除异常空白页
pdf
weixin_4383354023 分钟前
【示例】springboot实现json文件生成,压缩为zip文件并在浏览器下载
spring boot·后端·json·文件压缩
DKPT23 分钟前
查询服务器日志常用Linux命令有那些
java·linux·spring boot·spring·spring cloud
来杯@Java34 分钟前
计算机毕设JAVA——在线音乐播放网站(基于SpringBoot+Vue前后端分离的项目)
java·vue.js·spring boot·spring·课程设计
weixin_83686952039 分钟前
Spring Boot中使用Thymeleaf进行页面渲染
java·spring boot·后端
虫小宝44 分钟前
教程:Spring Boot中集成Memcached的详细步骤
spring boot·后端·memcached
Oh...Yeah!!!1 小时前
Maven高级的聚合和继承
java·开发语言·后端·maven
AskHarries1 小时前
Spring Boot集成DeepLearning4j实现图片数字识别
java·spring boot·后端
weixin_8368695201 小时前
Spring Boot与Apache Kafka的深度集成
spring boot·kafka·apache
eclipsercp2 小时前
《每天5分钟用Flask搭建一个管理系统》第9章:API设计
后端·python·flask