SpringBoot集成OpenPDF导出pdf

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

<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、编码

 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();

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值