|
@@ -7,6 +7,9 @@ import org.apache.commons.io.FileUtils;
|
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.Jsoup;
|
|
|
import org.jsoup.helper.W3CDom;
|
|
import org.jsoup.helper.W3CDom;
|
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.zefer.pd4ml.PD4Constants;
|
|
import org.zefer.pd4ml.PD4Constants;
|
|
|
import org.zefer.pd4ml.PD4ML;
|
|
import org.zefer.pd4ml.PD4ML;
|
|
|
|
|
|
|
@@ -15,7 +18,7 @@ import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
|
import java.io.StringReader;
|
|
import java.io.StringReader;
|
|
|
-import java.nio.file.FileSystems;
|
|
|
|
|
|
|
+import java.nio.file.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* https://pd4ml.com/api/org/zefer/pd4ml/PD4ML.html api文档
|
|
* https://pd4ml.com/api/org/zefer/pd4ml/PD4ML.html api文档
|
|
@@ -25,11 +28,30 @@ import java.nio.file.FileSystems;
|
|
|
* @author humuyu
|
|
* @author humuyu
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Component
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
-public class HtmlConvertPdf {
|
|
|
|
|
|
|
+public class HtmlConvertToPdf implements InitializingBean {
|
|
|
|
|
+ @Value("${html2Pdf.simsumPath}")
|
|
|
|
|
+ private String simsumPath;
|
|
|
|
|
+ @Value("${html2Pdf.simfangPath}")
|
|
|
|
|
+ private String simfangPath;
|
|
|
|
|
+ @Value("${html2Pdf.toolName}")
|
|
|
|
|
+ private String toolName;
|
|
|
private static final String CONVERT_TYPE_PD4ML = "PD4ML";
|
|
private static final String CONVERT_TYPE_PD4ML = "PD4ML";
|
|
|
- private static final String CONVERT_TYPE_OPENPDF = "OPENPDF";
|
|
|
|
|
- private static final String CONVERT_TYPE_CURRENT = CONVERT_TYPE_OPENPDF;
|
|
|
|
|
|
|
+ private static final String CONVERT_TYPE_OPENPDF = "OpenPDF";
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
|
|
+ String[] fontPath = {simsumPath, simfangPath};
|
|
|
|
|
+ for (String path : fontPath) {
|
|
|
|
|
+ String aimPath = path.substring(path.indexOf("/"));
|
|
|
|
|
+ Path temp = Paths.get(new File(HtmlConvertToPdf.class.getClassLoader().getResource("").getPath().concat(aimPath)).getAbsolutePath());
|
|
|
|
|
+ if (Files.exists(temp)) {
|
|
|
|
|
+ Files.delete(temp);
|
|
|
|
|
+ }
|
|
|
|
|
+ Files.copy(HtmlConvertToPdf.class.getClassLoader().getResourceAsStream(path), temp, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 将html转换成pdf
|
|
* 将html转换成pdf
|
|
@@ -38,10 +60,10 @@ public class HtmlConvertPdf {
|
|
|
* @param destPath
|
|
* @param destPath
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public static void converPdf(String html, String destPath) throws Exception {
|
|
|
|
|
- if (CONVERT_TYPE_OPENPDF.equals(CONVERT_TYPE_CURRENT)) {
|
|
|
|
|
|
|
+ public void converPdf(String html, String destPath) throws Exception {
|
|
|
|
|
+ if (CONVERT_TYPE_OPENPDF.equals(toolName)) {
|
|
|
convertPdfWithOpenPDF(html, destPath);
|
|
convertPdfWithOpenPDF(html, destPath);
|
|
|
- } else if (CONVERT_TYPE_PD4ML.equals(CONVERT_TYPE_CURRENT)) {
|
|
|
|
|
|
|
+ } else if (CONVERT_TYPE_PD4ML.equals(toolName)) {
|
|
|
convertPdfWithPD4ML(html, destPath);
|
|
convertPdfWithPD4ML(html, destPath);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -53,7 +75,7 @@ public class HtmlConvertPdf {
|
|
|
* @param destPath 输出的PDF文件路径
|
|
* @param destPath 输出的PDF文件路径
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- private static void convertPdfWithPD4ML(String html, String destPath) throws Exception {
|
|
|
|
|
|
|
+ private void convertPdfWithPD4ML(String html, String destPath) throws Exception {
|
|
|
File file = new File(destPath);
|
|
File file = new File(destPath);
|
|
|
FileUtils.writeStringToFile(file, "", "UTF-8");
|
|
FileUtils.writeStringToFile(file, "", "UTF-8");
|
|
|
FileOutputStream outFile = new FileOutputStream(file);
|
|
FileOutputStream outFile = new FileOutputStream(file);
|
|
@@ -77,7 +99,7 @@ public class HtmlConvertPdf {
|
|
|
* @param destPath
|
|
* @param destPath
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- private static void convertPdfWithOpenPDF(String html, String destPath) throws Exception {
|
|
|
|
|
|
|
+ private void convertPdfWithOpenPDF(String html, String destPath) throws Exception {
|
|
|
//加载html文件
|
|
//加载html文件
|
|
|
Document document = Jsoup.parse(html);
|
|
Document document = Jsoup.parse(html);
|
|
|
document.outputSettings().syntax(Document.OutputSettings.Syntax.html);
|
|
document.outputSettings().syntax(Document.OutputSettings.Syntax.html);
|
|
@@ -93,10 +115,11 @@ public class HtmlConvertPdf {
|
|
|
builder.withW3cDocument(new W3CDom().fromJsoup(document), baseUri);
|
|
builder.withW3cDocument(new W3CDom().fromJsoup(document), baseUri);
|
|
|
|
|
|
|
|
//引入指定字体,注意字体名需要和css样式中指定的字体名相同
|
|
//引入指定字体,注意字体名需要和css样式中指定的字体名相同
|
|
|
- String fontPath = HtmlConvertPdf.class.getClassLoader().getResource("").getPath().concat("font");
|
|
|
|
|
|
|
+ String fontPath = HtmlConvertToPdf.class.getClassLoader().getResource("").getPath();
|
|
|
System.out.println(fontPath);
|
|
System.out.println(fontPath);
|
|
|
- builder.useFont(new File(fontPath + "/simsun.ttf"), "SimSun", 1, BaseRendererBuilder.FontStyle.NORMAL, true);
|
|
|
|
|
- builder.useFont(new File(fontPath + "/simfang.ttf"), "simfang", 1, BaseRendererBuilder.FontStyle.NORMAL, true);
|
|
|
|
|
|
|
+ builder.useFont(new File(fontPath + simsumPath.substring(simsumPath.indexOf("/") + 1)), "SimSun", 400, BaseRendererBuilder.FontStyle.NORMAL, true);
|
|
|
|
|
+ builder.useFont(new File(fontPath + simfangPath.substring(simfangPath.indexOf("/") + 1)), "simfang", 400, BaseRendererBuilder.FontStyle.NORMAL, true);
|
|
|
|
|
+
|
|
|
builder.run();
|
|
builder.run();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|