In this step-by-step guide, we will explore how to generate PDF from HTML in Java. The HTML document can include text or graphics contents which will be rendered to PDF document with high fidelity. The following steps can be used to convert HTML to PDF using java in a simple and easy manner.
Steps to Generate PDF from HTML in Java
- Configure Aspose.Words for Java from Maven repository
- Import the Document class
- Load the input HTML file by specifying the file name
- Save the output PDF file
Here an input HTML file is loaded and the contents are converted from HTML to PDF using Java. You will see that the PDF contents are rendered the same as the HTML displayed in a modern browser.
Code to Convert HTML to PDF using Java
This Java code sample creates a PDF file from HTML without the requirement to install any browser or MS Office applications. The HTML to PDF conversion with the above example mimics the behavior of the MS Word application, i.e. you will get the same output if you export the HTML file to a PDF file with MS Word.
In this tutorial, we have learned how does Java create PDF from HTML, however, if you want to convert a PDF to Word format, refer to the article on how to convert PDF to Word in Java.