
If CSS and images are located in different directories, we need to use ConverterProperties to set the base URL. However, they need to be in the same location of the HTML file.

In this post we will use the following variations of the convertToPdf().convertToPdf() has different variations that takes HTML as String, File or InputStream & writes the PDF content to File, OutputStream or existing PdfDocument.We will use convertToPdf() method from HtmlConverter class to convert HTML to PDF. //Starting a new pdf document Document document new Document() ByteArrayOutputStream os new ByteArrayOutputStream() //This is your new pdf doc PdfWriter writer PdfWriter.getInstance(document, os) document.open() document.convertToPdf(): this method converts HTML to PDF.

convertToElements(): returns a list of iText IElement instances.convertToDocument(): returns Document instance.HtmlConverter class has three main methods with different inputs and return types:.HtmlConverter class is the main class to convert HTML to PDF.We need to add iText core library and pdfHTML add-on to our project dependencies.“When using iText PDF in a closed source environment, you will need to purchase an iText PDF commercial license.” Environment, Tools & Libraries used in this post
#Pdfwriter in java how to
This post shows how to use iText to convert HTML to PDF. iText has an add-on that enables converting HTML to PDF document.

Creating a PDF file from HTML can be done using iText Java library.
