日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

Java实现HTML代码生成PDF文档

發(fā)布時間:2023/12/9 HTML 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现HTML代码生成PDF文档 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java實(shí)現(xiàn)HTML代碼生成PDF文檔

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處http://blog.csdn.net/zdtwyjp/archive/2010/07/27/5769353.aspx

1、IText實(shí)現(xiàn)html2pdf,速度快,糾錯能力差,支持中文(要求HTML使用unicode編碼),但中支持一種中文字體,開源。

2、Flying Sauser實(shí)現(xiàn)html2pdf,糾錯能力差,支持多種中文字體(部分樣式不能識別),開源。

3、PD4ML實(shí)現(xiàn)html2pdf,速度快,糾錯能力強(qiáng),支持多種中文字體,商業(yè)。

(一)IText

官網(wǎng):http://www.itextpdf.com/測試案例:TestIText.java依賴jar包:iText-2.0.8.jar、iTextAsian.jar(支持中文)下面只是一個小的測試案例,如果項(xiàng)目中使用到了該組件可以參考API完成項(xiàng)目組中相應(yīng)的功能! view plaincopy to clipboardprint? 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("文檔創(chuàng)建成功"); }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("文檔創(chuàng)建成功"); }catch(Exception e) { e.printStackTrace(); } } } 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("文檔創(chuàng)建成功"); }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("文檔創(chuàng)建成功"); }catch(Exception e) { e.printStackTrace(); } } }

(二)Flying Sauser

項(xiàng)目主頁:https://xhtmlrenderer.dev.java.net/依賴jar包:iText-2.0.8.jar、iTextAsian.jar、core-renderer.jar默認(rèn)情況下,core-renderer.jar對中文是不能進(jìn)行換行的,如果想解決換行問題可以去http://bettereveryday.javaeye.com/blog/611561下載一個jar包,該包對源代碼做了稍加修改.下面只是一個小的測試案例,如果項(xiàng)目中使用到了該組件可以參考API完成項(xiàng)目組中相應(yīng)的功能! view plaincopy to clipboardprint? 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(); // DOCTYPE 必需寫否則類似于 這樣的字符解析會出現(xiàn)錯誤 html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("<head>") .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.getSharedContext().setBaseURL("file:/F:/teste/html/"); renderer.layout(); renderer.createPDF(os); os.close(); } } 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(); // DOCTYPE 必需寫否則類似于 這樣的字符解析會出現(xiàn)錯誤 html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("<head>").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.getSharedContext().setBaseURL("file:/F:/teste/html/"); renderer.layout(); renderer.createPDF(os); os.close(); } } http://bettereveryday.javaeye.com/blog/611561參考資料:http://yongboy.javaeye.com/blog/510976http://www.51itsns.com/sns/space.php?uid=4&do=blog&id=582關(guān)于Flying Sauser的一篇非常不錯的文章:http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

(三)PD4ML

官網(wǎng)下載:http://pd4ml.com/downloads.htm依賴jar包:pd4ml_demo.jar、pd4ml__css2.jar、fonts.jar下面只是一個小的測試案例,如果項(xiàng)目中使用到了該組件可以參考API完成項(xiàng)目組中相應(yīng)的功能!view plaincopy to clipboardprint?

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(““)
.append(““)
.append(“”)
.append(““)
.append(““)
.append(““)
.append(“顯示中文“)
.append(“
”)
.append(““);
StringReader strReader = new StringReader(html.toString());
converter.generatePDF_1(pdfFile, strReader);
}
// 手動構(gòu)造HTML代碼
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);
}

// HTML代碼來自于HTML文件 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); }

}
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(““)
.append(““)
.append(“”)
.append(““)
.append(““)
.append(““)
.append(“顯示中文“)
.append(“
”)
.append(““);
StringReader strReader = new StringReader(html.toString());
converter.generatePDF_1(pdfFile, strReader);
}
// 手動構(gòu)造HTML代碼
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);
}

// HTML代碼來自于HTML文件
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.htmhttp://www.pd4ml.com/api/index.htmlhttp://pd4ml.com/reference.htm#7.1http://pd4ml.com/support/html-pdf-faq-f1/double-byte-support-t195.htmlhttp://pd4ml.com/support/pd4ml-html-css-pdf-tips-tricks-f7/ttf-embedding-t42.html

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/zdtwyjp/archive/2010/07/27/5769353.aspx

總結(jié)

以上是生活随笔為你收集整理的Java实现HTML代码生成PDF文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。