使用 iText 将 HTML 转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/235851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using iText to convert HTML to PDF
提问by Mark
Does anyone know if it is possible to convert a HTML page (url) to a PDF using iText?
有谁知道是否可以使用 iText 将 HTML 页面(url)转换为 PDF?
If the answer is 'no' than that is OK as well since I will stop wasting my time trying to work it out and just spend some money on one of a number of components which I know can :)
如果答案是“否”,那也没关系,因为我将不再浪费时间尝试解决问题,而只需在我知道可以的多个组件之一上花一些钱:)
Thanks in advance for your responses!
预先感谢您的回复!
回答by opensas
I think this is exactly what you were looking for
我认为这正是你要找的
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
http://code.google.com/p/flying-saucer
http://code.google.com/p/flying-saucer
Flying Saucer's primary purpose is to render spec-compliant XHTML and CSS 2.1 to the screen as a Swing component. Though it was originally intended for embedding markup into desktop applications (things like the iTunes Music Store), Flying Saucer has been extended work with iText as well. This makes it very easy to render XHTML to PDFs, as well as to images and to the screen. Flying Saucer requires Java 1.4 or higher.
Flying Saucer 的主要目的是将符合规范的 XHTML 和 CSS 2.1 作为 Swing 组件呈现到屏幕上。虽然它最初是为了将标记嵌入桌面应用程序(例如 iTunes 音乐商店),但 Flying Saucer 也已扩展到 iText 中。这使得将 XHTML 呈现为 PDF 以及图像和屏幕变得非常容易。飞碟需要 Java 1.4 或更高版本。
回答by Mark
I have ended up using ABCPdf from webSupergoo. It works really well and for about $350 it has saved me hours and hours based on your comments above. Thanks again Daniel and Bratch for your comments.
我最终使用了来自 webSupergoo 的 ABCPdf。它工作得很好,大约 350 美元,根据您上面的评论,它为我节省了数小时。再次感谢 Daniel 和 Bratch 的评论。
回答by Joris Schellekens
The easiest way of doing this is using pdfHTML. It's an iText7 add-on that converts HTML5 (+CSS3) into pdf syntax.
最简单的方法是使用 pdfHTML。它是一个 iText7 插件,可将 HTML5 (+CSS3) 转换为 pdf 语法。
The code is pretty straightforward:
代码非常简单:
HtmlConverter.convertToPdf(
"<b>This text should be written in bold.</b>", // html to be converted
new PdfWriter(
new File("C://users/mark/documents/output.pdf") // destination file
)
);
To learn more, go to http://itextpdf.com/itext7/pdfHTML
要了解更多信息,请访问http://itextpdf.com/itext7/pdfHTML
回答by Jes
The answer to your question is actually two-fold. First of all you need to specify what you intend to do with the rendered HTML: save it to a new PDF file, or use it within another rendering context (i.e. add it to some other document you are generating).
你的问题的答案实际上有两个方面。首先,您需要指定您打算对呈现的 HTML 做什么:将它保存到一个新的 PDF 文件,或在另一个呈现上下文中使用它(即,将它添加到您正在生成的其他文档中)。
The former is relatively easily accomplished using the Flying Saucer framework, which can be found here: https://github.com/flyingsaucerproject/flyingsaucer
前者使用飞碟框架相对容易完成,可以在这里找到:https: //github.com/flyingsaucerproject/flyingsaucer
The latter is actually a much more comprehensive problem that needs to be categorized further.
Using iText you won't be able to (trivially, at least) combine iText elements (i.e. Paragraph
, Phrase
, Chunk
and so on) with the generated HTML. You can hack your way out of this by using the ContentByte
's addTemplate
method and generating the HTML to this template.
后者实际上是一个更全面的问题,需要进一步分类。利用iText你将不能够(平凡,至少)结合iText的元素(即Paragraph
,Phrase
,Chunk
等等)与生成的HTML。您可以通过使用ContentByte
'saddTemplate
方法并将 HTML 生成到此模板来解决此问题。
If you on the other hand want to stamp the generated HTML with something like watermarks, dates or the like, you can do this using iText.
另一方面,如果您想用水印、日期等标记生成的 HTML,您可以使用 iText。
So bottom line: You can't trivially integrate the rendered HTML in other pdf generating contexts, but you can render HTML directly to a blank PDF document.
所以底线:您不能简单地将呈现的 HTML 集成到其他 pdf 生成上下文中,但您可以将 HTML 直接呈现到空白 PDF 文档中。
回答by Asad Rao
Use itext libray:
使用 itext 库:
Here is the sample code. It is working perfectly fine:
这是示例代码。它工作得很好:
String htmlFilePath = filePath + ".html";
String pdfFilePath = filePath + ".pdf";
// create an html file on given file path
Writer unicodeFileWriter = new OutputStreamWriter(new FileOutputStream(htmlFilePath), "UTF-8");
unicodeFileWriter.write(document.toString());
unicodeFileWriter.close();
ConverterProperties properties = new ConverterProperties();
properties.setCharset("UTF-8");
if (url.contains(".kr") || url.contains(".tw") || url.contains(".cn") || url.contains(".jp")) {
properties.setFontProvider(new DefaultFontProvider(false, false, true));
}
// convert the html file to pdf file.
HtmlConverter.convertToPdf(new File(htmlFilePath), new File(pdfFilePath), properties);
Maven dependencies
Maven 依赖项
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.1.3</version>
</dependency>
回答by Mark Melville
When I needed HTML to PDF conversion earlier this year, I tried the trial of Winnovative HTML to PDF converter (I think ExpertPDF is the same product, too). It worked great so we bought a license at that company. I don't go into it too in depth after that.
今年早些时候,当我需要将 HTML 转换为 PDF 时,我尝试了 Winnovative HTML 到 PDF 转换器的试用版(我认为 ExpertPDF 也是同一产品)。效果很好,所以我们在该公司购买了许可证。之后我就不再深入探讨了。