Java Itext 实现HTML 转换PDF

本文介绍了一种使用itextpdf库将HTML文件转换为PDF的方法,包括处理中文内容的技术细节,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目编码:

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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值