智能仲裁后端服务

PdfUtils.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.PageSize;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. import com.itextpdf.tool.xml.XMLWorkerHelper;
  9. import java.io.*;
  10. import java.nio.charset.Charset;
  11. import java.nio.file.Files;
  12. /**
  13. * @author wangqiong
  14. * @description pdf转换工具类
  15. * @date 2023-11-28 15:20
  16. */
  17. public class PdfUtils {
  18. /**
  19. * 将html字符串转为pdf
  20. * @param pdfFilePath 保存的路径
  21. * @param htmlcontent:必须是完整的html格式,比如<html><body>123</body></html>
  22. */
  23. public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
  24. Document document = new Document();
  25. PdfWriter writer = null;
  26. try {
  27. if(!new File(pdfFilePath).exists()){
  28. new File(pdfFilePath).createNewFile();
  29. }
  30. writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
  31. // 设置底部距离60,解决重叠问题
  32. document.setPageSize(PageSize.A4);
  33. document.setMargins(50, 45, 50, 60);
  34. document.setMarginMirroring(false);
  35. document.open();
  36. XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), null, Charset.forName("UTF-8"));
  37. } catch (Exception e) {
  38. return false;
  39. } finally {
  40. if (null != document) {
  41. document.close();
  42. }
  43. if (null != writer) {
  44. writer.close();
  45. }
  46. }
  47. return true;
  48. }
  49. /**
  50. * 根据html文件生成pdf
  51. * @param pdfFilePath pdf文件生成路径
  52. * @param htmlFilePath html文件路径
  53. */
  54. public static boolean htmlFileConvertToPDF(String pdfFilePath, String htmlFilePath) {
  55. Document document = new Document();
  56. PdfWriter writer = null;
  57. FileOutputStream fileOutputStream = null;
  58. FileInputStream fileInputStream = null;
  59. try {
  60. fileOutputStream = new FileOutputStream(pdfFilePath);
  61. writer = PdfWriter.getInstance(document, fileOutputStream);
  62. // 设置底部距离60,解决重叠问题
  63. document.setPageSize(PageSize.A4);
  64. document.setMargins(50, 45, 50, 60);
  65. document.setMarginMirroring(false);
  66. document.open();
  67. StringBuffer sb = new StringBuffer();
  68. fileInputStream = new FileInputStream(htmlFilePath);
  69. BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
  70. String readStr = "";
  71. while ((readStr = br.readLine()) != null) {
  72. sb.append(readStr);
  73. }
  74. XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8"));
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. } finally {
  78. if (null != document) {
  79. document.close();
  80. }
  81. if (null != writer) {
  82. writer.close();
  83. }
  84. if (null != fileInputStream) {
  85. try {
  86. fileInputStream.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. if (null != fileOutputStream) {
  92. try {
  93. fileOutputStream.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. /**
  102. * docx转pdf
  103. * @param pdfSaveDirectory:文件保存目录,例:D:\\pdf\\
  104. * @param fileName:保存的文件名称,例:test.pdf
  105. * @param docxFile:需要转换的docx文件
  106. * @return
  107. */
  108. private static boolean docxConvertToPDF(String pdfSaveDirectory,String fileName,File docxFile) {
  109. // 不存在则新建
  110. File directory = new File(pdfSaveDirectory);
  111. if (!directory.exists()) {
  112. directory.mkdirs();
  113. }
  114. String pdfFilePath = pdfSaveDirectory + fileName;
  115. File outputFile = new File(pdfFilePath);
  116. try {
  117. InputStream docxInputStream = Files.newInputStream(docxFile.toPath());
  118. OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
  119. IConverter converter = LocalConverter.builder().build();
  120. converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
  121. docxInputStream.close();
  122. outputStream.close();
  123. } catch (Exception e) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. public static void main(String[] args) {
  129. try {
  130. String pdfFile = "D:\\test2.pdf";
  131. String htmlContent1 = "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2>";
  132. String htmlContent = "<html><body style=\"font-size:12.0pt; font-family:宋体\">" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
  133. String htmlContent2 = "<html><body >" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
  134. htmlStringConvertToPDF(pdfFile,htmlContent);
  135. // parseHtml2PdfByFilePath(pdfFile,htmlFile);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }