How to create a PDF file using java and iText

 

In this article, I am going to explain how to create a PDF file using open source iText library, the first thing is you need to load below dependencies in your maven project pom.xml file

The below code has several helper methods which helps us to create content or adding metadata to the PDF file, 

Adding metadata to the PDF

The below code adds metadata to the PDF file 

Add paragraph 

The below code takes text and the alignment as an input parameter and add it to the document object

Read HTML File and convert into PDF

The below code takes HTML file path and style sheet path as input parameters and render it in the PDF file 

Pass HTML as a String 

The below code takes html string as input parameter

Final code is something like below

Now, create another class called PDFManager and add below code

The output would be something like below, the welcome text is added through paragraph method, the links below are added by passing HTML text and the main content is read from the external HTML file

html2pdf

Download Code

 pdf exporter