智能仲裁后端服务

PdfUtils.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.ruoyi.common.utils;
  2. import com.documents4j.api.DocumentType;
  3. import com.documents4j.api.IConverter;
  4. import com.documents4j.job.LocalConverter;
  5. import com.itextpdf.text.Document;
  6. import com.itextpdf.text.Font;
  7. import com.itextpdf.text.PageSize;
  8. import com.itextpdf.text.pdf.BaseFont;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10. import com.itextpdf.tool.xml.XMLWorkerFontProvider;
  11. import com.itextpdf.tool.xml.XMLWorkerHelper;
  12. import com.ruoyi.common.config.RuoYiConfig;
  13. import com.tencentcloudapi.teo.v20220901.models.CC;
  14. import lombok.extern.slf4j.Slf4j;
  15. import java.io.*;
  16. import java.nio.charset.Charset;
  17. import java.nio.file.Files;
  18. import static cn.hutool.core.util.ClassLoaderUtil.getClassLoader;
  19. /**
  20. * @author wangqiong
  21. * @description pdf转换工具类
  22. * @date 2023-11-28 15:20
  23. */
  24. @Slf4j
  25. public class PdfUtils {
  26. /**
  27. * 将html字符串转为pdf
  28. * @param pdfFilePath 保存的路径
  29. * @param htmlcontent:必须是完整的html格式,比如<html><body>123</body></html>
  30. */
  31. public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
  32. Document document = new Document();
  33. PdfWriter writer = null;
  34. try {
  35. writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
  36. // 设置底部距离60,解决重叠问题
  37. document.setPageSize(PageSize.A4);
  38. document.setMargins(50, 45, 50, 60);
  39. document.setMarginMirroring(false);
  40. document.open();
  41. // 解决PDF中文不显示
  42. String fontPath = "/D:/simsun.ttf"; //字体文件路径
  43. XMLWorkerFontProvider provider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
  44. provider.register(fontPath);//注册字体
  45. log.error("注册字体");
  46. XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), Charset.forName("UTF-8"), provider);
  47. } catch (Exception e) {
  48. log.error("htmlStringConvertToPDF error", e.getMessage());
  49. return false;
  50. } finally {
  51. if (null != document) {
  52. document.close();
  53. }
  54. if (null != writer) {
  55. writer.close();
  56. }
  57. }
  58. return true;
  59. }
  60. /**
  61. * 根据html文件生成pdf
  62. * @param pdfFilePath pdf文件生成路径
  63. * @param htmlFilePath html文件路径
  64. */
  65. public static boolean htmlFileConvertToPDF(String pdfFilePath, String htmlFilePath) {
  66. Document document = new Document();
  67. PdfWriter writer = null;
  68. FileOutputStream fileOutputStream = null;
  69. FileInputStream fileInputStream = null;
  70. try {
  71. fileOutputStream = new FileOutputStream(pdfFilePath);
  72. writer = PdfWriter.getInstance(document, fileOutputStream);
  73. // 设置底部距离60,解决重叠问题
  74. document.setPageSize(PageSize.A4);
  75. document.setMargins(50, 45, 50, 60);
  76. document.setMarginMirroring(false);
  77. document.open();
  78. StringBuffer sb = new StringBuffer();
  79. fileInputStream = new FileInputStream(htmlFilePath);
  80. BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
  81. String readStr = "";
  82. while ((readStr = br.readLine()) != null) {
  83. sb.append(readStr);
  84. }
  85. XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8"));
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. } finally {
  89. if (null != document) {
  90. document.close();
  91. }
  92. if (null != writer) {
  93. writer.close();
  94. }
  95. if (null != fileInputStream) {
  96. try {
  97. fileInputStream.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. if (null != fileOutputStream) {
  103. try {
  104. fileOutputStream.close();
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * docx转pdf
  114. * @param pdfSaveDirectory:文件保存目录,例:D:\\pdf\\
  115. * @param fileName:保存的文件名称,例:test.pdf
  116. * @param docxFile:需要转换的docx文件
  117. * @return
  118. */
  119. private static boolean docxConvertToPDF(String pdfSaveDirectory,String fileName,File docxFile) {
  120. // 不存在则新建
  121. File directory = new File(pdfSaveDirectory);
  122. if (!directory.exists()) {
  123. directory.mkdirs();
  124. }
  125. String pdfFilePath = pdfSaveDirectory + fileName;
  126. File outputFile = new File(pdfFilePath);
  127. try {
  128. InputStream docxInputStream = Files.newInputStream(docxFile.toPath());
  129. OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
  130. IConverter converter = LocalConverter.builder().build();
  131. converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
  132. docxInputStream.close();
  133. outputStream.close();
  134. } catch (Exception e) {
  135. return false;
  136. }
  137. return true;
  138. }
  139. }