标签: java html 代码| 发表时间:2016-05-13 07:40 | 作者:hunan84229247
出处:http://www.iteye.com
http://blog.csdn.net/zdtwyjp/article/details/5769353
http://www.xuebuyuan.com/2056017.html
1、IText实现html2pdf,速度快,纠错能力差,支持中文(要求HTML使用unicode编码),但中支持一种中文字体,开源。
2、Flying Sauser实现html2pdf,纠错能力差,支持多种中文字体(部分样式不能识别),开源。
3、PD4ML实现html2pdf,速度快,纠错能力强,支持多种中文字体,商业。
(一)IText
官网: http://www.itextpdf.com/
测试案例:TestIText.java
依赖jar包:iText-2.0.8.jar、iTextAsian.jar(支持中文)
下面只是一个小的测试案例,如果项目中使用到了该组件可以参考API完成项目组中相应的功能!
[c-sharp] view plain copy
- 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
- 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 Sauser
项目主页: 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完成项目组中相应的功能!
[c-sharp] view plain copy
- 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 必需写否则类似于 这样的字符解析会出现错误
- html.append("nbsp;html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">");
- html.append("").append("")
- .append("")
- .append(" ")
- .append("")
- .append("");
- html.append("支持中文!");
- html.append("");
- 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/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完成项目组中相应的功能!
[java] view plain copy
- 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);
- }
- // 手动构造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.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文档的方案大致就这些了,希望能够给大家带来帮助!如果上面的三种方案都还不能满足项目组的需求哪就只有去买商业软件了。
已有 0人发表留言,猛击->> 这里ITeye推荐- —软件人才免语言低担保 赴美带薪读研!—

HTML is not only the skeleton of web pages, but is more widely used in many fields: 1. In web page development, HTML defines the page structure and combines CSS and JavaScript to achieve rich interfaces. 2. In mobile application development, HTML5 supports offline storage and geolocation functions. 3. In emails and newsletters, HTML improves the format and multimedia effects of emails. 4. In game development, HTML5's Canvas API is used to create 2D and 3D games.

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
