|
@@ -1,13 +1,21 @@
|
|
|
package me.zhengjie.base.util;
|
|
package me.zhengjie.base.util;
|
|
|
|
|
|
|
|
|
|
+import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder;
|
|
|
|
|
+import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
+import org.jsoup.Jsoup;
|
|
|
|
|
+import org.jsoup.helper.W3CDom;
|
|
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
import org.zefer.pd4ml.PD4Constants;
|
|
import org.zefer.pd4ml.PD4Constants;
|
|
|
import org.zefer.pd4ml.PD4ML;
|
|
import org.zefer.pd4ml.PD4ML;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
import java.awt.*;
|
|
|
-import java.io.*;
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
+import java.io.StringReader;
|
|
|
|
|
+import java.nio.file.FileSystems;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* https://pd4ml.com/api/org/zefer/pd4ml/PD4ML.html api文档
|
|
* https://pd4ml.com/api/org/zefer/pd4ml/PD4ML.html api文档
|
|
@@ -19,78 +27,43 @@ import java.io.*;
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class HtmlConvertPdf {
|
|
public class HtmlConvertPdf {
|
|
|
-
|
|
|
|
|
- private static int width = 585;
|
|
|
|
|
- private static int height = 822;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取PDF的宽度
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public int getWidth() {
|
|
|
|
|
- return width;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 设置PDF的宽度
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public void setWidth(int width) {
|
|
|
|
|
- HtmlConvertPdf.width = width;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取PDF的高度
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public int getHeight() {
|
|
|
|
|
- return height;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 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;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 设置PDF的高度
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
|
|
+ * 将html转换成pdf
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param html
|
|
|
|
|
+ * @param destPath
|
|
|
|
|
+ * @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public void setHeight(int height) {
|
|
|
|
|
- HtmlConvertPdf.height = height;
|
|
|
|
|
|
|
+ public static void converPdf(String html, String destPath) throws Exception {
|
|
|
|
|
+ if (CONVERT_TYPE_OPENPDF.equals(CONVERT_TYPE_CURRENT)) {
|
|
|
|
|
+ convertPdfWithOpenPDF(html, destPath);
|
|
|
|
|
+ } else if (CONVERT_TYPE_PD4ML.equals(CONVERT_TYPE_CURRENT)) {
|
|
|
|
|
+ convertPdfWithPD4ML(html, destPath);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 创建PDF文件
|
|
|
|
|
- *
|
|
|
|
|
- * @param html html字符串
|
|
|
|
|
- * @param desPath 输出的PDF文件路径
|
|
|
|
|
|
|
+ * 使用PD4ML将html转换成pdf
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param html html字符串
|
|
|
|
|
+ * @param destPath 输出的PDF文件路径
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public static void createPdfFile(String html, String desPath) throws Exception {
|
|
|
|
|
- File file = new File(desPath);
|
|
|
|
|
|
|
+ private static void convertPdfWithPD4ML(String html, String destPath) throws Exception {
|
|
|
|
|
+ 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);
|
|
|
|
|
+ //
|
|
|
PD4ML pdf = new PD4ML();
|
|
PD4ML pdf = new PD4ML();
|
|
|
- // 设置html的宽度
|
|
|
|
|
- pdf.setHtmlWidth(width);
|
|
|
|
|
- // 设置字体的数据
|
|
|
|
|
- int topValue = 20;
|
|
|
|
|
- int leftValue = 15;
|
|
|
|
|
- int rightValue = 10;
|
|
|
|
|
- int bottomValue = 25;
|
|
|
|
|
-
|
|
|
|
|
- pdf.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
|
|
|
|
|
- // 设置页面的标准
|
|
|
|
|
|
|
+ pdf.setHtmlWidth(585);
|
|
|
|
|
+ pdf.setPageInsetsMM(new Insets(20, 15, 25, 10));
|
|
|
pdf.setPageSizeMM(PD4Constants.A4);
|
|
pdf.setPageSizeMM(PD4Constants.A4);
|
|
|
- // 设置字体
|
|
|
|
|
pdf.useTTF("java:fonts", true);
|
|
pdf.useTTF("java:fonts", true);
|
|
|
- // pd4ml.useTTF("F:/Test/fonts", true); //F:/Test/fonts文件夹可自定义,保持一致即可
|
|
|
|
|
-// String path = AppRun.class.getResource("/fonts").getPath();
|
|
|
|
|
-// System.out.println("pd4fonts字体:" + path);
|
|
|
|
|
-// pdf.useTTF(path, true);
|
|
|
|
|
- // 设置默认的字体
|
|
|
|
|
pdf.setDefaultTTFs("simfang", "simfang", "simfang");
|
|
pdf.setDefaultTTFs("simfang", "simfang", "simfang");
|
|
|
- // 设置HTML全局样式
|
|
|
|
|
pdf.addStyle("body,html {margin: 0 auto;}", true);
|
|
pdf.addStyle("body,html {margin: 0 auto;}", true);
|
|
|
// 测试开启,上线的时候关闭
|
|
// 测试开启,上线的时候关闭
|
|
|
pdf.enableDebugInfo();
|
|
pdf.enableDebugInfo();
|
|
@@ -98,45 +71,31 @@ public class HtmlConvertPdf {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 读取文件路径
|
|
|
|
|
- *
|
|
|
|
|
- * @param path 路径
|
|
|
|
|
- * @param encoding 编码
|
|
|
|
|
- * @return string
|
|
|
|
|
- * @throws IOException
|
|
|
|
|
|
|
+ * 使用OpenPDF将html转换成pdf
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param html
|
|
|
|
|
+ * @param destPath
|
|
|
|
|
+ * @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public String readFile(String path, String encoding) throws IOException {
|
|
|
|
|
-
|
|
|
|
|
- FileInputStream is = new FileInputStream(path);
|
|
|
|
|
- BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
|
|
- ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
|
|
|
|
- byte[] buffer = new byte[2048];
|
|
|
|
|
- int read;
|
|
|
|
|
- do {
|
|
|
|
|
- read = is.read(buffer, 0, buffer.length);
|
|
|
|
|
- if (read > 0) {
|
|
|
|
|
- fos.write(buffer, 0, read);
|
|
|
|
|
- }
|
|
|
|
|
- } while (read > -1);
|
|
|
|
|
-
|
|
|
|
|
- fos.close();
|
|
|
|
|
- bis.close();
|
|
|
|
|
- is.close();
|
|
|
|
|
-
|
|
|
|
|
- return fos.toString(encoding);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
- System.out.println(HtmlConvertPdf.class.getResource("/fonts").getPath());
|
|
|
|
|
- ;
|
|
|
|
|
-
|
|
|
|
|
-// HtmlConvertPdf util = new HtmlConvertPdf();
|
|
|
|
|
-// try {
|
|
|
|
|
-// String str = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
-// String html = util.readFile("/Users/bakerzp/Downloads/other/html/promise.html", "utf-8");
|
|
|
|
|
-// createPdfFile(html, "/Users/bakerzp/Downloads/other/" + str + ".pdf");
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// e.printStackTrace();
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+ private static void convertPdfWithOpenPDF(String html, String destPath) throws Exception {
|
|
|
|
|
+ //加载html文件
|
|
|
|
|
+ Document document = Jsoup.parse(html);
|
|
|
|
|
+ document.outputSettings().syntax(Document.OutputSettings.Syntax.html);
|
|
|
|
|
+ //引入资源目录,可以单独引入css,图片文件等
|
|
|
|
|
+ String baseUri = FileSystems.getDefault().getPath("eladmin-system/src/main/resources/css").toUri().toString();
|
|
|
|
|
+ //
|
|
|
|
|
+ File file = new File(destPath);
|
|
|
|
|
+ FileUtils.writeStringToFile(file, "", "UTF-8");
|
|
|
|
|
+ OutputStream os = new FileOutputStream(file);
|
|
|
|
|
+ PdfRendererBuilder builder = new PdfRendererBuilder();
|
|
|
|
|
+ builder.withUri(destPath);
|
|
|
|
|
+ builder.toStream(os);
|
|
|
|
|
+ builder.withW3cDocument(new W3CDom().fromJsoup(document), baseUri);
|
|
|
|
|
+
|
|
|
|
|
+ //引入指定字体,注意字体名需要和css样式中指定的字体名相同
|
|
|
|
|
+ String fontPath = HtmlConvertPdf.class.getClassLoader().getResource("").getPath().concat("font");
|
|
|
|
|
+ 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.run();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|