简体   繁体   English

将HTML文件转换为PDF

[英]Converting HTML files into PDF

I am using the following code to generate a PDF file of the HTML Report 我正在使用以下代码来生成HTML报告的PDF文件

String url = new File("Test.html").toURI().toURL().toString();
OutputStream os = new FileOutputStream("Test.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();

I was able to use it on sample HTML files to convert to pdf. 我能够在示例HTML文件上使用它来转换为pdf。 But when it comes to my real usage, the HTML content consists of various special symbols, like &,<,> that can't be parsed by XML . 但是,就我的实际用法而言, HTML内容由各种特殊符号组成,例如&,<,> ,而XML无法解析。

I tried using CDATA, while generating HTML itself, but later found that the text around CDATA is not visible in HMTL. 我尝试使用CDATA生成HTML本身,但是后来发现CDATA周围的文本在HMTL中不可见。

Does anyone have a solution for this? 有人对此有解决方案吗?

Have you tried to print to pdf from the browser? 您是否尝试从浏览器打印为pdf? Google primo pdf for a program that we'll let you do it. Google primo pdf程序,我们将为您服务。

I don't know if this will help you, but you can use StringEscapeUtils from apache-commons . 我不知道这是否对您有帮助,但是您可以使用apache-commons StringEscapeUtils It has methods for escape and unescape HTML (you may use them to pre-process your HTML before PDF generation). 它具有转义和转义HTML (您可以使用它们在PDF生成之前对HTML进行预处理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM