Convert HTML to PDF using iText

[url]http://www.rgagnon.com/javadetails/java-html-to-pdf-using-itext.html[/url]

Using iText HTMLWorker, you can produce PDF version of an HTML document. The document must be simple. Many things like FORM elements or external images are not supported.

This was done with iText 2.1.3. If using a newer version like 5.1.0, you need to adjust the import :

import com.lowagie....; --> import com.itextpdf....;

import java.io.FileOutputStream;
import java.io.StringReader;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;

public class HtmlToPDF {

public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("c://temp//testpdf.pdf"));
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addCreationDate();
document.addTitle("Please read this");

HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<html><head></head><body>"+
"<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
"<h1>Show your support</h1>" +
"<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees, " +
"in personal hardware and software costs to set up test environments, and above all," +
"the huge amounts of time it takes for one person to design and write the actual content." +
"<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?" +
"<p>Donate using PayPal® to real@rgagnon.com." +
"<p>Contributions via PayPal are accepted in any amount " +
"<P><br><table border='1'><tr><td>Java HowTo<tr>" +
"<td bgcolor='red'>Javascript HowTo<tr><td>Powerbuilder HowTo</table>" +
"</body></html>";
htmlWorker.parse(new StringReader(str));
document.close();
System.out.println("Done");
}
catch (Exception e) {
e.printStackTrace();
}
}
}



As you can see, the validity of the parsed HTML (using HTMLWorker) is very relax. Closing tags are not required : <BR> is ok (<BR/> is not mandatory).
HTMLWorker is ok with older iText version but the recommended approach with new iText is to use the XMLWorker. XMLWorker is stricter since you must send XHTML document to it.

import java.io.FileOutputStream;
import java.io.StringReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class HtmlToPDF {

public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance
(document, new FileOutputStream("c://temp//testpdf.pdf"));
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addCreationDate();
document.addTitle("Please read this");

XMLWorkerHelper worker = new XMLWorkerHelper();

String str = "<html><head></head><body>"+
"<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
"<h1>Show your support</h1>" +
"<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees, " +
"in personal hardware and software costs to set up test environments, and above all," +
"the huge amounts of time it takes for one person to design and write the actual content.</p>" +
"<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>" +
"<p>Donate using PayPal® to real@rgagnon.com.</p>" +
"<p>Contributions via PayPal are accepted in any amount</p>" +
"<P><br><table border='1'><tr><td>Java HowTo</td></tr><tr>" +
"<td style='background-color:red;'>Javascript HowTo</td></tr>" +
"<tr><td>Powerbuilder HowTo</td></tr></table>" +
"</body></html>";
worker.parseXHtml(pdfWriter, document, new StringReader(str));
document.close();
System.out.println("Done.");
}
catch (Exception e) {
e.printStackTrace();
}
}

}

Note : To get the color on a table cell, you need to use a style because the bgcolor attribute is not supported. By default, FORM elements are not rendered. Review this document to see what is supported : http://demo.itextsupport.com/xmlworker/doc.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值