Generate PDF from HTML in java

In this tutorial, we will learn how to generate a PDF file from the HTML file in Java programming. HTML helps us to generate web pages. We will create a String variable and store our HTML code in it. And using FileOutputStream we will create a new pdf file ie. Output.pdf to display our HTML code.

Instead of using the String variable we can also use the created HTML file.

For writing this code we require several jar-files ie. itextpdf-5.4.0.jar and xmlworker-5.4.1.jar

Link to download itextpdf-5.4.0.jar::https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.4.0

Link to download xmlworker-5.4.1.jar::https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker/5.4.1

To generate PDF file from HTML in java

We can generate our code using any IDE ie. Notepad, Notepad++, Netbeans, Eclipse. But we will write this code using Eclipse as using Eclipse and Notepad it becomes easy for us to add the required jar files to our code. We can directly add our jar files using the Libraries and Buildpath option. But using Notepad++ and Notepad it becomes a very lengthy process as we need to add jar files to the classpath of our computer.

Code

Below is our Java code

package html2pdf;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.StringReader;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Document;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class NewClass1 {
  private static Object XMLWorkerHelper;
    public static void main(String args[]){
    try {
    	String k="<html><body>CodeSpeedy</body></html>";
    	FileOutputStream file=new FileOutputStream(new File("output1.pdf"));
    	Document document=new Document();
    	PdfWriter writer=PdfWriter.getInstance(document, file);
    	document.open();
    	ByteArrayInputStream is=new ByteArrayInputStream(k.getBytes());
    	((com.itextpdf.tool.xml.XMLWorkerHelper) XMLWorkerHelper).getInstance().parseXHtml(writer,document,is);
    	document.close();
    	file.close();
    	System.out.println("done");
    }catch(Exception e) {
    	e.printStackTrace();
    }
    	
    	
    }

}

In the above code html2pdf is the name of our package. We imported all the required packages and interfaces. In the main method of our class, we created a String variable k to store the HTML code.

Then we used FileOutputStream to generate a file name output.pdf  and we will display our data in output.pdf as the output of our code.

The Document is further used to parse the given InputStream and return a new DOM object. Then we passed the document instance to the PDFWriter along with the name of the pdf file.

To write data in output.pdf we first need to open the document. And we cannot write data to the file after closing the document. Then we used to print the data from HTML file to PDF file using ByteArrayInputStream. We used parseXHtml() to parse the HTML data and send the created element to the Element Handler. This parse method may give error using Netbeans therefore, we used Eclipse for writing the code.

Further, we closed the opened files and documents. and printed “done” as our output.

Also, read: Adding text to PDF file in Java

Leave a Reply

Your email address will not be published. Required fields are marked *