Do you need to convert HTML to PDF? In this post, we use a page from Google Play as an example to showcase our advanced HTML conversion hero, pdfHTML, versus a competitor’s HTML converter.
Below is the original web page we’re using to test iText 7 pdfHTML:
Here is the result of iText 7’s pdfHTML conversion, versus a competitor’s efforts:
With iText 7’s pdfHTML conversion, the table formatting was preserved, and all the images and data were perfectly aligned as they should appear.
But that's not all. pdfHTML also provides a convenient and simple API (application programming interface) for Java and .NET (C#) which allows you to convert HTML files straight to PDF files, or to a list of iText elements. This gives you full control over how to parse and insert the HTML elements. By re-using the structural information from HTML to easily create PDF/A, PDF/UA, or Tagged PDFs, pdfHTML provides the engine to convert HTML to PDF, and the results are well-formatted, well-structured PDF documents. There is no need to know the technical details of the PDF format, pdfHTML utilizes semantic and structural information already present in the HTML.
*Under Section 508 of the Rehabilitation Act of 1973, PDF/UA is a requirement for all PDF files.
Usage example
The following code demonstrates how to generate a PDF from an HTML source:
1
2
3
4
5
6
7
8
9
public static void main() throws IOException {
// IO
File htmlSource = new File("input.html");
File pdfDest = new File("output.pdf");
// pdfHTML specific code
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(new FileInputStream(htmlSource),
new FileOutputStream(pdfDest), converterProperties);
}
1
2
3
4
5
6
7
8
9
static void Main(string[] args)
{
using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
using (FileStream pdfDest = File.Open("output.pdf", FileMode.OpenOrCreate))
{
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
}
}
Want to see more? Read the full pdfHTML ebook on our Knowledge Base!