一、IText實現html2pdf,速度快,糾錯能力差,支持中文(要求HTML使用unicode編碼),但中支持一種中文字體,開源。php
二、Flying Sauser實現html2pdf,糾錯能力差,支持多種中文字體(部分樣式不能識別),開源。css
三、PD4ML實現html2pdf,速度快,糾錯能力強,支持多種中文字體,商業。html
(一)ITextjava
官網:http://www.itextpdf.com/api
測試案例:TestIText.Javaapp
依賴jar包:iText-2.0.8.jar、iTextAsian.jar(支持中文)測試
下面只是一個小的測試案例,若是項目中使用到了該組件能夠參考API完成項目組中相應的功能!字體
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.util.ArrayList;
- import com.lowagie.text.Document;
- import com.lowagie.text.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.html.simpleparser.HTMLWorker;
- import com.lowagie.text.html.simpleparser.StyleSheet;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- public class TestIText{
- public static void main(String[] args) {
- TestIText ih = new TestIText();
- ih.htmlCodeComeFromFile("D://Test//iText.html", "D://Test//iText_1.pdf");
- ih.htmlCodeComeString("Hello中文", "D://Test//iText_2.pdf");
- }
-
- public void htmlCodeComeFromFile(String filePath, String pdfPath) {
- Document document = new Document();
- try {
- StyleSheet st = new StyleSheet();
- st.loadTagStyle("body", "leading", "16,0");
- PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
- document.open();
- ArrayList p = HTMLWorker.parseToList(new FileReader(filePath), st);
- for(int k = 0; k < p.size(); ++k) {
- document.add((Element)p.get(k));
- }
- document.close();
- System.out.println("文檔建立成功");
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- public void htmlCodeComeString(String htmlCode, String pdfPath) {
- Document doc = new Document(PageSize.A4);
- try {
- PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
- doc.open();
-
- BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
- Paragraph t = new Paragraph(htmlCode, FontChinese);
- doc.add(t);
- doc.close();
- System.out.println("文檔建立成功");
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
(二)Flying Sauserui
項目主頁:https://xhtmlrenderer.dev.java.net/編碼
依賴jar包:iText-2.0.8.jar、iTextAsian.jar、core-renderer.jar
默認狀況下,core-renderer.jar對中文是不能進行換行的,若是想解決換行問題能夠去http://bettereveryday.javaeye.com/blog/611561下載一個jar包,該包對源代碼作了稍加修改.
下面只是一個小的測試案例,若是項目中使用到了該組件能夠參考API完成項目組中相應的功能!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
-
- import org.xhtmlrenderer.pdf.ITextFontResolver;
- import org.xhtmlrenderer.pdf.ITextRenderer;
-
- import com.lowagie.text.pdf.BaseFont;
-
- public class TestFlyingSauser {
- public static void main(String[] args) throws Exception {
- demo_1();
- demo_2();
- }
-
-
- public static void demo_1() throws Exception {
- String inputFile = "D:/Test/flying.html";
- String url = new File(inputFile).toURI().toURL().toString();
- String outputFile = "D:/Test/flying.pdf";
- OutputStream os = new FileOutputStream(outputFile);
- ITextRenderer renderer = new ITextRenderer();
- renderer.setDocument(url);
- renderer.layout();
- renderer.createPDF(os);
- os.close();
- }
-
-
- public static void demo_2() throws Exception {
- String outputFile = "D:/Test/demo_3.pdf";
- OutputStream os = new FileOutputStream(outputFile);
- ITextRenderer renderer = new ITextRenderer();
- ITextFontResolver fontResolver = renderer.getFontResolver();
- fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- StringBuffer html = new StringBuffer();
-
- html.append("<!DOCTYPE html PUBLIC /"-
- html.append("<html xmlns=/"http:
- .append("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=UTF-8/" />")
- .append("<mce:style type=/"text/css/"><!--
- body {font-family: SimSun;}
- --></mce:style><style type=/"text/css/" mce_bogus="1">body {font-family: SimSun;}</style>")
- .append("</head>")
- .append("<body>");
- html.append("<div>支持中文!</div>");
- html.append("</body></html>");
- renderer.setDocumentFromString(html.toString());
-
-
- renderer.layout();
- renderer.createPDF(os);
- os.close();
- }
- }
http://bettereveryday.javaeye.com/blog/611561
參考資料:http://yongboy.javaeye.com/blog/510976
http://www.51itsns.com/sns/space.php?uid=4&do=blog&id=582
關於Flying Sauser的一篇很是不錯的文章:http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
(三)PD4ML
官網下載:http://pd4ml.com/downloads.htm
依賴jar包:pd4ml_demo.jar、pd4ml__css2.jar、fonts.jar
下面只是一個小的測試案例,若是項目中使用到了該組件能夠參考API完成項目組中相應的功能!
- import java.awt.Insets;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.StringReader;
-
- import org.zefer.pd4ml.PD4Constants;
- import org.zefer.pd4ml.PD4ML;
-
- public class Converter {
- public static void main(String[] args) throws Exception {
- Converter converter = new Converter();
- converter.generatePDF_2(new File("D:/Test/demo_ch_pd4ml_a.pdf"), "D:/Test/a.htm");
- File pdfFile = new File("D:/Test/demo_ch_pd4ml.pdf");
- StringBuffer html = new StringBuffer();
- html.append("<html>")
- .append("<head>")
- .append("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=UTF-8/" />")
- .append("</head>")
- .append("<body>")
- .append("<font face=/"KaiTi_GB2312/">")
- .append("<font color='red' size=22>顯示中文</font>")
- .append("</font>")
- .append("</body></html>");
- StringReader strReader = new StringReader(html.toString());
- converter.generatePDF_1(pdfFile, strReader);
- }
-
- public void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception {
- FileOutputStream fos = new FileOutputStream(outputPDFFile);
- PD4ML pd4ml = new PD4ML();
- pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
- pd4ml.setHtmlWidth(950);
- pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
- pd4ml.useTTF("java:fonts", true);
- pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
- pd4ml.enableDebugInfo();
- pd4ml.render(strReader, fos);
- }
-
-
- public void generatePDF_2(File outputPDFFile, String inputHTMLFileName) throws Exception {
- FileOutputStream fos = new FileOutputStream(outputPDFFile);
- PD4ML pd4ml = new PD4ML();
- pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
- pd4ml.setHtmlWidth(950);
- pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
- pd4ml.useTTF("java:fonts", true);
- pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
- pd4ml.enableDebugInfo();
- pd4ml.render("file:" + inputHTMLFileName, fos);
- }
- }
參考資料:
http://www.pd4ml.com/examples.htm
http://www.pd4ml.com/api/index.html
http://pd4ml.com/reference.htm#7.1
http://pd4ml.com/support/html-pdf-faq-f1/double-byte-support-t195.html
http://pd4ml.com/support/pd4ml-html-css-pdf-tips-tricks-f7/ttf-embedding-t42.html
生成PDF文檔的方案大體就這些了,但願可以給你們帶來幫助!若是上面的三種方案都還不能知足項目組的需求哪就只有去買商業軟件了。
轉載自--http://blog.csdn.net/zdtwyjp/article/details/5769353#