|
|
@@ -4,40 +4,56 @@ import com.documents4j.api.DocumentType;
|
|
4
|
4
|
import com.documents4j.api.IConverter;
|
|
5
|
5
|
import com.documents4j.job.LocalConverter;
|
|
6
|
6
|
import com.itextpdf.text.Document;
|
|
|
7
|
+import com.itextpdf.text.Font;
|
|
7
|
8
|
import com.itextpdf.text.PageSize;
|
|
|
9
|
+import com.itextpdf.text.pdf.BaseFont;
|
|
8
|
10
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
11
|
+import com.itextpdf.tool.xml.XMLWorkerFontProvider;
|
|
9
|
12
|
import com.itextpdf.tool.xml.XMLWorkerHelper;
|
|
|
13
|
+import com.ruoyi.common.config.RuoYiConfig;
|
|
|
14
|
+import com.tencentcloudapi.teo.v20220901.models.CC;
|
|
|
15
|
+import lombok.extern.slf4j.Slf4j;
|
|
10
|
16
|
|
|
11
|
17
|
import java.io.*;
|
|
12
|
18
|
import java.nio.charset.Charset;
|
|
13
|
19
|
import java.nio.file.Files;
|
|
14
|
20
|
|
|
|
21
|
+import static cn.hutool.core.util.ClassLoaderUtil.getClassLoader;
|
|
|
22
|
+
|
|
15
|
23
|
/**
|
|
16
|
24
|
* @author wangqiong
|
|
17
|
25
|
* @description pdf转换工具类
|
|
18
|
26
|
* @date 2023-11-28 15:20
|
|
19
|
27
|
*/
|
|
|
28
|
+@Slf4j
|
|
20
|
29
|
public class PdfUtils {
|
|
21
|
|
- /**
|
|
|
30
|
+ /**
|
|
22
|
31
|
* 将html字符串转为pdf
|
|
23
|
32
|
* @param pdfFilePath 保存的路径
|
|
24
|
33
|
* @param htmlcontent:必须是完整的html格式,比如<html><body>123</body></html>
|
|
25
|
34
|
*/
|
|
26
|
|
- public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
|
|
|
35
|
+ public boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
|
|
27
|
36
|
Document document = new Document();
|
|
28
|
37
|
PdfWriter writer = null;
|
|
29
|
38
|
try {
|
|
30
|
|
- if(!new File(pdfFilePath).exists()){
|
|
31
|
|
- new File(pdfFilePath).createNewFile();
|
|
32
|
|
- }
|
|
|
39
|
+// if(!new File(pdfFilePath).exists()){
|
|
|
40
|
+// new File(pdfFilePath).createNewFile();
|
|
|
41
|
+// }
|
|
33
|
42
|
writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
|
|
34
|
43
|
// 设置底部距离60,解决重叠问题
|
|
|
44
|
+
|
|
35
|
45
|
document.setPageSize(PageSize.A4);
|
|
36
|
46
|
document.setMargins(50, 45, 50, 60);
|
|
37
|
47
|
document.setMarginMirroring(false);
|
|
38
|
48
|
document.open();
|
|
39
|
|
- XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), null, Charset.forName("UTF-8"));
|
|
|
49
|
+ // 解决PDF中文不显示
|
|
|
50
|
+ String fontPath = "/D:/simsun.ttf"; //字体文件路径
|
|
|
51
|
+ XMLWorkerFontProvider provider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
|
|
|
52
|
+ provider.register(fontPath);//注册字体
|
|
|
53
|
+ log.error("注册字体");
|
|
|
54
|
+ XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), Charset.forName("UTF-8"), provider);
|
|
40
|
55
|
} catch (Exception e) {
|
|
|
56
|
+ log.error("htmlStringConvertToPDF error", e.getMessage());
|
|
41
|
57
|
return false;
|
|
42
|
58
|
} finally {
|
|
43
|
59
|
if (null != document) {
|
|
|
@@ -131,18 +147,4 @@ public class PdfUtils {
|
|
131
|
147
|
return true;
|
|
132
|
148
|
}
|
|
133
|
149
|
|
|
134
|
|
- public static void main(String [] args) {
|
|
135
|
|
- try {
|
|
136
|
|
-
|
|
137
|
|
- String pdfFile = "D:\\test13.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 style=\\\"font-size:12.0pt; font-family:宋体\\\"><h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
|
|
141
|
|
-
|
|
142
|
|
- htmlStringConvertToPDF(pdfFile,htmlContent2);
|
|
143
|
|
-// parseHtml2PdfByFilePath(pdfFile,htmlFile);
|
|
144
|
|
- } catch (Exception e) {
|
|
145
|
|
- e.printStackTrace();
|
|
146
|
|
- }
|
|
147
|
|
- }
|
|
148
|
150
|
}
|