}
30 // 手动构造HTML代码
31 public static void generatePDF_1(File outputPDFFile, StringReader strReader)
32 throws Exception {
33 FileOutputStream fos = new FileOutputStream(outputPDFFile);
34 PD4ML pd4ml = new PD4ML();
35 pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
36 pd4ml.setHtmlWidth(950);
37 pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
38 pd4ml.useTTF("java:fonts", true);
39 //pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
40 pd4ml.setDefaultTTFs("KaiTi", "KaiTi", "KaiTi");
41 pd4ml.enableDebugInfo();
42 pd4ml.render(strReader, fos);
43 }
45 // HTML代码来自于HTML文件
46 public static void generatePDF_2(File outputPDFFile, String inputHTMLFileName)
47 throws Exception {
48 FileOutputStream fos = new FileOutputStream(outputPDFFile);
49 PD4ML pd4ml = new PD4ML();
50 pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
51 pd4ml.setHtmlWidth(950);
52 pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
54 pd4ml.useTTF("java:fonts", true);
55 pd4ml.setDefaultTTFs("KaiTi", "KaiTi", "KaiTi");
56 pd4ml.enableDebugInfo();
57 pd4ml.render("file:" + inputHTMLFileName, fos);
58 }
支持URL、支持文件。支持中文,对html文件几乎无要求。样式不佳,中文字体支持不佳。
引入Java包:PDFGenerator.jar
1 PDFGenerator pdfGenerator=new PDFGenerator();
2 pdfGenerator.setMarginsForStandardPageFormat (10,10,10,10);
3 // pdfGenerator.setCharset ("utf-8");
4 pdfGenerator.setCharset("ISO-10646-UCS-2");
5 // pdfGenerator.generatePDFFromURL ("https://blog.csdn.net/gisboygogogo/article/details/77601308",
6 pdfGenerator.generatePDFFromURL ("F:\\pdf\\1.html",
7 "F:\\pdf/pdfgenerator-test1.pdf",
8 "A4", "Portrait");
样式问题:
具体实现:
1 // URL url = new URL("https://www.baidu.com/");
2 // PageFormat pf = new PageFormat();
3 // PDFDocument pdfDoc = PDFDocument.loadHTML (url, pf, true);
4 // pdfDoc.saveDocument ("F:\\pdf\\output.pdf");
5 File f1 = new File ("F:\\pdf\\1.html");
6 PDFDocument pdfDoc = PDFDocument.loadHTML(f1.toURI().toURL(), new PageFormat (), false);
7 pdfDoc.saveDocument ("F:\\pdf\\output.pdf");
并不是很好,虽然支持中文
仅支持URL,但能很好地转换,效果较好,速度较快。但收费,且为第三方服务,或有信息安全性问题。
如果不考虑html文件安全性的话,可以考虑。
以转CSDN及百度搜索网页为测试例。
官网:https://restpack.io/
能很好保持样式,且支持中文,速度快。价格相比较优惠。
https://www.cnblogs.com/IT-study/p/13738157.html
官网:https://pdfshift.io
能很好保持样式,且支持中文,速度快。价格相比较优惠。
1 String encoding = Base64.getEncoder().encodeToString("YOUR_API_KEY:".getBytes());
2 HttpPost httppost = new HttpPost("https://api.pdfshift.io/v2/convert/");
3 httppost.setHeader("Authorization", "Basic " + encoding);
4 httppost.setHeader("Content-type", "application/json");
6 HttpEntity postingString = new StringEntity("{\"source\":\"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=PhantomJS%20html%E8%BD%ACpdf&oq=PhantomJS&rsv_pq=c942451400041f65&rsv_t=3566cYExdLkZv6pJRhDXeda3WgHs37R3GASuPvnEGrOmBl9Lur2EMGyfdF8&rqlang=cn&rsv_enter=1&rsv_sug3=15&rsv_sug1=4&rsv_sug7=100&rsv_sug2=0&inputT=6104&rsv_sug4=6105\",\"landscape\":false,\"use_print\":false}");
7 httppost.setEntity(postingString);
9 CloseableHttpClient client = HttpClients.createDefault();
10 try (CloseableHttpResponse response = client.execute(httppost)) {
11 HttpEntity entity = response.getEntity();
12 // Use the entity
1. pdfmyurl(网站反应慢)
官网:http://pdfmyurl.com/
转化效率极低,速度极其慢
2. pdflayer(不支持中文)
官网:https://pdflayer.com/
不支持中文。虽然能很好保持样式。
个人总结:
到现在未知并为找到完美的方案。各种方案都有缺点,但是在线转换的方案转化效率以及对CSS等支持程度比较好。也不知道他们这些内部是如何实现的。
http://blog.csdn.net/ouyhong123/article/details/26401967
http://blog.csdn.net/tengdazhang770960436/article/details/41320079
http://www.cnblogs.com/jasondan/p/4108263.html
http://blog.csdn.net/accountwcx/article/details/46785437
http://blog.csdn.net/zdtwyjp/article/details/5769353