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