Java Itext 实现HTML 转换PDF

深蓝计划 专栏收录该内容
78 篇文章 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
    点赞
  • 1
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值