Itext将HTML文件转换成PDFITeye - AG环亚娱乐集团

Itext将HTML文件转换成PDFITeye

2019年03月09日08时42分12秒 | 作者: 旭鹏 | 标签: 文件,创立,获取 | 浏览: 1883

Itext将HTML文件转化成PDF

1.需求下载 itext.jar包,xmlworker-5.5.4.zip,这些包都能在sourceforge中找到!


package com.pdfbox.pdf;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 * HTML转化PDF
 * @author LGF 2015-01-04
public class HTMLToPDF {
 public static void main(String[] args) throws Exception {
 //创立文档目标
 Document doc = new Document();
 //创立输出流
 OutputStream out = new FileOutputStream("src/html.pdf");
 //获取PdfWriter实例
 PdfWriter writer = PdfWriter.getInstance(doc, out);
 //打开文档
 doc.open();
 //获取 XMLWorkerHelper实例
 XMLWorkerHelper work = XMLWorkerHelper.getInstance();
 //解析html文件,创立pdf文档
 work.parseXHtml(writer, doc, getInputStream("pdf.html"));
 //开释资源
 doc.close();
 writer.close();
 out.close();
 System.out.println("success");
 * 获取 class path 中的文件流
 * @param name 称号
 * @return InputStream 
 public static InputStream getInputStream(String name){
 return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);

 

 !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" 
 html 
 head 
 title HTML to PDF /title 
 style type="text/css" 
 color:#ccc;
table tr td{
 text-align:center;
 border:1px solid gray;
 padding:4px;
table tr th{
 background-color:#84C7FD;
 color:#fff;
 width: 100px;
.itext{
 color:#84C7FD;
 font-weight:bold;
.description{
 color:gray;
 /style 
 /head 
 body 
 h1 HTML to PDF /h1 
 span itext 5.4.2 /span 
 span converting HTML to PDF /span 
 table 
 th Title /th 
 td iText - Java HTML to PDF /td 
 /tr 
 th URL /th 
 td http://hmkcode.com/itext-html-to-pdf-using-java /td 
 /tr 
 /table 
 img src="http://dl2.iteye.com/upload/attachment/0083/2873/9d920e8c-8f09-395a-986a-e7a49911676c.png"/ 
 /body 
 /html 

 
 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章