Java Itext 实现HTML 转换PDF

你猜 阅读:743 2021-03-31 20:58:34 评论:0

项目编码:

1、依赖文件

2、核心编码

<!--集成itext --> 
		<dependency> 
			<groupId>com.itextpdf</groupId> 
			<artifactId>itextpdf</artifactId> 
			<version>5.5.13</version> 
		</dependency> 
		<dependency> 
			<groupId>com.itextpdf.tool</groupId> 
			<artifactId>xmlworker</artifactId> 
			<version>5.5.13</version> 
		</dependency> 
		 <dependency> 
            <groupId>com.itextpdf</groupId> 
            <artifactId>itext-asian</artifactId> 
            <version>5.2.0</version> 
        </dependency>
package com.zzg.html.trans.pdf; 
 
import java.io.ByteArrayInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.nio.charset.Charset; 
 
import com.itextpdf.text.Document; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.tool.xml.XMLWorkerHelper; 
 
public class HtmlTransPDF { 
	/** 
	 *  
	 * @Title: htmlTransPdf    
	 * @Description: html 转 pdf ,简单字符和数字   
	 * @param: @param inputStream 
	 * @param: @param outputStream       
	 * @return: void       
	 * @throws 
	 */ 
	public static void htmlTransPdf(InputStream inputStream, OutputStream outputStream){ 
		try{ 
			 Document document = new Document(); 
	            // 为该Document创建一个Writer实例 
	            PdfWriter pdfwriter = PdfWriter.getInstance(document, 
	            		outputStream); 
	            pdfwriter.setViewerPreferences(PdfWriter.HideToolbar); 
	            // 打开当前的document 
	            document.open(); 
	            XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document,inputStream); 
	            document.close(); 
 
		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
	} 
	 
	/** 
	 *  
	 * @Title: htmlTransPdfChinese    
	 * @Description: html 转 pdf, 简单中文 
	 * @param: @param pdfFile 
	 * @param: @param content       
	 * @return: void       
	 * @throws 
	 */ 
	public static void htmlTransPdfChinese(String pdfFile, String content){ 
		try{ 
			 Document document = new Document(); 
	         // 为该Document创建一个Writer实例 
	         PdfWriter pdfwriter = PdfWriter.getInstance(document, 
	        		 new FileOutputStream(pdfFile)); 
	         pdfwriter.setViewerPreferences(PdfWriter.HideToolbar); 
	         // 打开当前的document 
	         document.open(); 
	         XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document,new ByteArrayInputStream(content.getBytes("Utf-8")),Charset.forName("UTF-8")); 
	         document.close(); 
 
		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
	} 
	 
	 
} 

测试代码:

package com.zzg.html.trans.pdf.test; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
 
import com.zzg.html.trans.pdf.HtmlTransPDF; 
 
public class HtmlTransPDFTest { 
 
//	public static void main(String[] args) { 
//		// TODO Auto-generated method stub 
//		String htmlPath = "c:\\image\\2.html"; 
//		String pdfPath ="c:\\image\\2.pdf"; 
//		File htmlFile = new File(htmlPath); 
//		File pdfFile = new File(pdfPath); 
//		if(htmlFile.exists()){ 
//			if(!pdfFile.exists()){ 
//				try { 
//					pdfFile.createNewFile(); 
//				} catch (IOException e) { 
//					// TODO Auto-generated catch block 
//					e.printStackTrace(); 
//				} 
//			} 
//			// html开始转换 
//			try { 
//				HtmlTransPDF.htmlTransPdf(new FileInputStream(htmlFile), new FileOutputStream(pdfFile)); 
//			} catch (FileNotFoundException e) { 
//				// TODO Auto-generated catch block 
//				e.printStackTrace(); 
//			} 
//		} 
//	} 
	 
	public static void main(String[] args) { 
		String htmlPath = "C:\\image\\2.html"; 
		String pdfPath = "C:\\image\\3.pdf"; 
		String content = ""; 
		File htmlFile = new File(htmlPath); 
		File pdfFile = new File(pdfPath); 
		if(htmlFile.exists()){ 
			if(!pdfFile.exists()){ 
				try { 
					pdfFile.createNewFile(); 
				} catch (IOException e) { 
					// TODO Auto-generated catch block 
					e.printStackTrace(); 
				} 
			} 
			// 开始读取html 文件内容 
			BufferedReader br; 
			try { 
				br = new BufferedReader(new InputStreamReader( 
						new FileInputStream(htmlFile), "UTF-8")); 
				String row = ""; 
				while ((row = br.readLine()) != null) { 
					// System.out.println(t); 
					content += row; 
				} 
				HtmlTransPDF.htmlTransPdfChinese(pdfPath, content); 
			} catch (UnsupportedEncodingException e) { 
				// TODO Auto-generated catch block 
				e.printStackTrace(); 
			} catch (FileNotFoundException e) { 
				// TODO Auto-generated catch block 
				e.printStackTrace(); 
			} catch (IOException e) { 
				// TODO Auto-generated catch block 
				e.printStackTrace(); 
			} 
			 
 
		} 
	} 
 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号