java html 导出 pdf文件,Java HTML导出PDF (一)

转自:http://blog..net/zdtwyjp/article/details/5769353

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

plaincopy

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;

publicclassTestIText{

publicstaticvoidmain(String[] args) {

TestIText ih = newTestIText();

ih.htmlCodeComeFromFile("D://Test//iText.html","D://Test//iText_1.pdf");

ih.htmlCodeComeString("Hello中文","D://Test//iText_2.pdf");

}

publicvoidhtmlCodeComeFromFile(String filePath, String pdfPath) {

Document document = newDocument();

try{

StyleSheet st = newStyleSheet();

st.loadTagStyle("body","leading","16,0");

PdfWriter.getInstance(document, newFileOutputStream(pdfPath));

document.open();

ArrayList p = HTMLWorker.parseToList(newFileReader(filePath), st);

for(intk = 0; k 

document.add((Element)p.get(k));

}

document.close();

System.out.println("文档创建成功");

}catch(Exception e) {

e.printStackTrace();

}

}

publicvoidhtmlCodeComeString(String htmlCode, String pdfPath) {

Document doc = newDocument(PageSize.A4);

try{

PdfWriter.getInstance(doc, newFileOutputStream(pdfPath));

doc.open();

// 解决中文问题

BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

Font FontChinese = newFont(bfChinese, 12, Font.NORMAL);

Paragraph t = newParagraph(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

plaincopy

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;

publicclassTestFlyingSauser {

publicstaticvoidmain(String[] args) throws Exception {

demo_1();

demo_2();

}

// 不支持中文

publicstaticvoiddemo_1() throws Exception {

String inputFile = "D:/Test/flying.html";

String url = newFile(inputFile).toURI().toURL().toString();

String outputFile = "D:/Test/flying.pdf";

OutputStream os = newFileOutputStream(outputFile);

ITextRenderer renderer = newITextRenderer();

renderer.setDocument(url);

renderer.layout();

renderer.createPDF(os);

os.close();

}

// 支持中文

publicstaticvoiddemo_2() throws Exception {

String outputFile = "D:/Test/demo_3.pdf";

OutputStream os = newFileOutputStream(outputFile);

ITextRenderer renderer = newITextRenderer();

ITextFontResolver fontResolver = renderer.getFontResolver();

fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

StringBuffer html = newStringBuffer();

// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误

html.append("html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">");

html.append("").append("

")

.append("")

.append("

body {font-family: SimSun;}

-->")

.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

plaincopy

importjava.awt.Insets;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.StringReader;

importorg.zefer.pd4ml.PD4Constants;

importorg.zefer.pd4ml.PD4ML;

publicclassConverter {

publicstaticvoidmain(String[] args)throwsException {

Converter converter = newConverter();

converter.generatePDF_2(newFile("D:/Test/demo_ch_pd4ml_a.pdf"),"D:/Test/a.htm");

File pdfFile = newFile("D:/Test/demo_ch_pd4ml.pdf");

StringBuffer html = newStringBuffer();

html.append("")

.append("

")

.append("")

.append("")

.append("

")

.append("")

.append("显示中文")

.append("")

.append("");

StringReader strReader = newStringReader(html.toString());

converter.generatePDF_1(pdfFile, strReader);

}

// 手动构造HTML代码

publicvoidgeneratePDF_1(File outputPDFFile, StringReader strReader)throwsException {

FileOutputStream fos = newFileOutputStream(outputPDFFile);

PD4ML pd4ml = newPD4ML();

pd4ml.setPageInsets(newInsets(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文件

publicvoidgeneratePDF_2(File outputPDFFile, String inputHTMLFileName)throwsException {

FileOutputStream fos = newFileOutputStream(outputPDFFile);

PD4ML pd4ml = newPD4ML();

pd4ml.setPageInsets(newInsets(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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值