java html转pdf 无法支持中文_java项目实现html转pdf的需求(支持中文和CSS样式)

java项目中用到了html转pdf的需求,现在写一个自己认为最优方案的总结,虽然还是有一些小的bug

为了保证中文的支持需要在被转换的html添加body的添加中文字体样式,保持这个字体和后边转换pdf的字体一致就可以了,不需要必须是SimSun字体

body{

font-family:SimSun;

}

下面放上jar包的引用pom

注意:org.xhtmlrenderer.core-renderer.R8的jar包,其实需要做一些修改,直接用的话不支持中文换行,可以点击下载支持中文换行的jar包,修改jar包名字替掉R8的jar包

org.xhtmlrenderer

core-renderer

R8

org.apache.commons

commons-lang3

3.0

com.itextpdf

itextpdf

5.5.10

com.itextpdf

html2pdf

2.0.0

这种方式对html里的代码样式要求比较严格,类似xhtml的标准,要求html中的标签都是闭合的,像meta和link这样的标签以及空格 这类的转义符都会导致转换pdf报错,因此最好在之前将html代码进行处理,以下是处理代码:

import java.io.File;

import java.io.FileInputStream;

import com.itextpdf.html2pdf.jsoup.Jsoup;

import com.itextpdf.html2pdf.jsoup.nodes.Document;

import com.itextpdf.html2pdf.jsoup.nodes.Entities;

public class HtmlToXHtmlJsoup {

public static String html2xhtml(String html) {

Document doc = Jsoup.parse(html);

doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);

return doc.html();

}

public static void main(String[] args) throws Exception {

// File file = new File("E:\\html2xhtml.html");

File file = new File("report01.html");

FileInputStream input = new FileInputStream(file);

int size = input.available();

byte[] buff = new byte[size];

input.read(buff);

input.close();

String html = new String(buff, "utf-8");

System.out.println("============html===========

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要将HTML换为PDF时保留HTML中的样式,可以使用CSS来定义样式,然后将CSS文件链接到HTML文件中。iText库会自动将CSS样式应用于PDF生成文件。 以下是一个示例代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class HtmlToPdfWithStyle { public static void main(String[] args) throws Exception { String inputFile = "input.html"; String cssFile = "style.css"; String outputFile = "output.pdf"; // Create a Document object Document document = new Document(); // Create a PdfWriter object PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); // Open the Document document.open(); // Read the HTML file into an InputStream InputStream htmlStream = new FileInputStream(inputFile); // Read the CSS file into an InputStream InputStream cssStream = new FileInputStream(cssFile); // Use the XMLWorkerHelper to parse the HTML and generate PDF XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlStream, cssStream); // Close the Document document.close(); } } ``` 在上面的示例代码中,您需要将“input.html”替换为您要换的HTML文件的路径,并将“style.css”替换为您的CSS文件的路径。在HTML文件中,您需要使用<link>标签将CSS文件链接到HTML文件中: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML to PDF</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>Hello, World!</h1> <p>This is a paragraph.</p> </body> </html> ``` 在CSS文件中,您可以定义样式: ```css h1 { color: red; } p { font-size: 14pt; } ``` 这将应用于换后的PDF文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值