• SpringBoot集成OpenPDF导出pdf


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

    1. com.github.librepdf
    2. openpdf
    3. 1.3.34
    4. com.github.librepdf
    5. openpdf-fonts-extra
    6. 1.3.34

    2、编码

    1. String outputPath = "d:\\pdf" + File.separator;
    2. File file = new File(outputPath);
    3. if (!file.exists()) {
    4. file.mkdir();
    5. }
    6. outputPath = outputPath + "hello.pdf";
    7. FileOutputStream fileOutputStream = new FileOutputStream(outputPath);
    8. // 设置字体
    9. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    10. Font titleFont = new Font(bfChinese, 13, Font.BOLD, Color.BLACK);
    11. Font docFont = new Font(bfChinese, 10, Font.UNDEFINED, Color.BLACK);
    12. Document document = new Document(PageSize.A4);
    13. PdfWriter.getInstance(document, fileOutputStream);
    14. document.open();
    15. // 初始化一个4列的表格,超过自动换行
    16. PdfPTable table = new PdfPTable(4);
    17. table.setWidthPercentage(100f);
    18. table.setSpacingAfter(20f);
    19. Paragraph paragraph = new Paragraph("检查表名称", titleFont);
    20. PdfPCell cell = new PdfPCell(paragraph);
    21. cell.setFixedHeight(20F);
    22. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    23. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    24. cell.setColspan(4);
    25. table.addCell(cell);
    26. paragraph = new Paragraph("检查人", docFont);
    27. cell = new PdfPCell(paragraph);
    28. cell.setFixedHeight(20F);
    29. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    30. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    31. table.addCell(cell);
    32. cell = new PdfPCell(new Paragraph(taskResp.getOrgUserName(), docFont));
    33. cell.setFixedHeight(20F);
    34. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    35. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    36. table.addCell(cell);
    37. cell = new PdfPCell(new Paragraph("检查时间", docFont));
    38. cell.setFixedHeight(20F);
    39. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    40. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    41. table.addCell(cell);
    42. cell = new PdfPCell(new Paragraph(TimeUtil.get_yyyyMMddHHmmss(taskResp.getCompleteTime()), docFont));
    43. cell.setFixedHeight(20F);
    44. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    45. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    46. table.addCell(cell);
    47. cell = new PdfPCell(new Paragraph("检查事项", docFont));
    48. cell.setFixedHeight(20F);
    49. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    50. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    51. table.addCell(cell);
    52. cell = new PdfPCell(new Paragraph("检查内容", docFont));
    53. cell.setFixedHeight(20F);
    54. cell.setColspan(2);
    55. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    56. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    57. table.addCell(cell);
    58. cell = new PdfPCell(new Paragraph("检查意见", docFont));
    59. cell.setFixedHeight(20F);
    60. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    61. cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
    62. table.addCell(cell);
    63. // 循环处理数据,可删除
    64. for (InspectItemMoldResp inspectItemMoldResp : inspectItemResp.getMoldList()) {
    65. for (int i = 0; i < inspectItemMoldResp.getPatternList().size(); i++) {
    66. if (i == 0) {
    67. cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getName(), docFont));
    68. // cell.setFixedHeight(inspectItemMoldResp.getPatternList().size() * 20F);
    69. cell.setRowspan(inspectItemMoldResp.getPatternList().size());
    70. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
    71. cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置居中对齐
    72. table.addCell(cell);
    73. cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
    74. cell.setFixedHeight(20F);
    75. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    76. cell.setColspan(2);
    77. table.addCell(cell);
    78. cell = new PdfPCell(new Paragraph(
    79. StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
    80. .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
    81. .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
    82. cell.setFixedHeight(20F);
    83. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    84. table.addCell(cell);
    85. } else {
    86. cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
    87. cell.setFixedHeight(20F);
    88. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    89. cell.setColspan(2);
    90. table.addCell(cell);
    91. cell = new PdfPCell(new Paragraph(
    92. StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
    93. .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
    94. .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
    95. cell.setFixedHeight(20F);
    96. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    97. table.addCell(cell);
    98. }
    99. }
    100. }
    101. document.add(table);
    102. document.close();// 关闭文档
    103. fileOutputStream.flush();
    104. fileOutputStream.close();

  • 相关阅读:
    为您的视频编辑应用添加动力,美摄视频剪辑SDK
    websocket协议原理
    CodeMirror 创建标签计算编辑器
    C/C++内存管理
    虹科示波器 | 汽车免拆检测 | 2017款长安福特翼虎车发动机故障灯异常点亮
    叶子数和深度
    java lombok
    Kubernetes 应用容器化
    java游戏制作-拼图游戏
    java 整合 swagger-ui 步骤
  • 原文地址:https://blog.csdn.net/weixin_44990255/article/details/139807419