Java itextpdf工具包 HTML转PDF(支持中文)

0.设置本地字体文件

字体获得:‪Windows系统:C:\Windows\Fonts\,拷贝过去就行
在这里插入图片描述

1.源码

pom坐标

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.0</version>
		</dependency>

工具类

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

/**
 * @description html转pdf 支持中文内容
 * @date 2023/11/9
 */

public class HtmlToPdfUtils {
    
    

	private static final Logger log = Logger.getLogger(HtmlToPdfUtils.class.getName());

	private HtmlToPdfUtils() {
    
    
	}

	/**
	 * 创建包含单词的PDF
	 *
	 * @param htmlStr 字符串
	 * @param pdfPath pdf路径
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void creatHtmlpdf(String htmlStr, String pdfPath) throws IOException, DocumentException {
    
    
		// step 1 new Document 默认大小A4
		Document document = new Document(PageSize.A4.rotate());
		// step 2
		PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(pdfPath)));
		// step 3
		document.open();
		// step 4
		Paragraph context = new Paragraph();
		ElementList elementList = parseToElementList(htmlStr, null);
		context.addAll(elementList);
		document.add(context);
		// step 5
		document.close();
	}

	/**
	 * 设置字体信息
	 *
	 * @return
	 */
	private static Font getFontInf() {
    
    
		// 字体路径
		String fontPath = "src/main/webapp/font/simhei.ttf";
		BaseFont baseFont = null;
		Font font = null;
		try {
    
    
			// 设置字体路径,字体编码,是否将字体嵌入pdf(默认false)
			baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			// 设置默认字体数据
			font = new Font(baseFont, 12f, Font.NORMAL, BaseColor.BLACK);
		} catch (DocumentException | IOException e) {
    
    
			log.log(Level.SEVERE, "获取pdf字体信息失败", e);
		}
		return font;
	}

	/**
	 * html转pdf 写法
	 *
	 * @param html
	 * @param css
	 * @return
	 * @throws IOException
	 */
	public static ElementList parseToElementList(String html, String css) throws IOException {
    
    
		// CSS
		CSSResolver cssResolver = new StyleAttrCSSResolver();
		if (css != null) {
    
    
			CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(css.getBytes()));
			cssResolver.addCss(cssFile);
		}

		// HTML
		MyFontsProvider fontProvider = new MyFontsProvider();
		CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
		HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
		htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
		htmlContext.autoBookmark(false);

		// Pipelines
		ElementList elements = new ElementList();
		ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
		HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
		CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

		// XML Worker
		XMLWorker worker = new XMLWorker(cssPipeline, true);
		XMLParser p = new XMLParser(worker);
		html = html.replace("<br>", "").replace("<hr>", "").replace("<img>", "").replace("<param>", "").replace("<link>", "");
		p.parse(new ByteArrayInputStream(html.getBytes()));
		return elements;
	}

	static class MyFontsProvider extends XMLWorkerFontProvider {
    
    
		public MyFontsProvider() {
    
    
			super(null, null);
		}

		@Override
		public Font getFont(final String fontname, String encoding, float size, final int style) {
    
    
			return getFontInf();
		}
	}


	public static void main(String[] args) throws DocumentException, IOException {
    
    
		String htmlPath = "D:\\工作文件夹\\index.html"; // 输入HTML文件的路径
		String pdfPath = "D:\\工作文件夹\\index.pdf"; // 输出PDF文件的路径
		// 获取html内容文字
		String htmlContent = new String(Files.readAllBytes(Paths.get(htmlPath)));
		creatHtmlpdf(htmlContent, pdfPath);
	}
}


2.HTML范例:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
      
      
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!啊啊啊啊啊啊啊啊啊啊啊啊</h1>
<h1>Welcome to nginx!字體文件不存在,影響倒出pdf中文顯示</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

在这里插入图片描述
转出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Ying_ph/article/details/134314131
今日推荐