DEV Community

Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

Java Generate PDF From HTML (Without Losing Formatting)

Java is one of the most used programming languages for large-scale and enterprise level applications. The PDF format, which stands for portable document format, provides people with an easy, dependable way to present and exchange documents - regardless of the software, hardware, or operating systems used by anyone viewing the document. Hence pdf is a widely used format to generate documents in Software Applications.

HTML is used to represent the information in a web browser. Therefore every information, report, invoice, or web page of the application is in HTML. Hence, we need to learn to generate pdf files from HTML. In this tutorial, we will learn to convert an HTML file into a pdf file.

We need a third-party library to convert HTML files into pdf files. There are multiple libraries are available with their pros and cons. Some of them are paid, some do not provide enough functionalities, some of them do not convert HTML files with 100% accuracy and some of them have performance issues.

I have found the IronPDF java library ideal for this purpose, as it is free for development, more secure, provides all functionalities in the single library with 100& accuracy, and has excellent performance.

Before moving forward, Let's have a brief introduction about IronPDF.

IronPDF:

IronPDF is the most popular Java PDF library developed by IronSoftware for creating and editing PDFs. Its simple API enables developers to create professional, high-quality PDFs from HTML in a variety of Java projects including Scala and Kotlin. IronPDF communicates with the IronPdfEngine via gRPC.

The main features include the creation of PDF documents using HTML, HTTP, JavaScript, CSS, and a variety of image formats. We can also add headers and footers, signatures, attachments, passwords, and security. It provides complete multithreading support with a tonne more!

Now we will start code examples.

First of all, we need to create a new maven project.

Create a New Project:

Steps for creating a new project in Java may differ from IDE to IDE. I am using InetlliJ IDE, you can use any. Open Intelli J IDE. Click on File > New > Project. A new window will appear as shown below.
Image description
Name your project, select location, select language, Build System, and JDK. Click on Create Button. A new project will be created.

Now, we will install IronPDF in our java application.

Install IronPDF java library

We need to define IronPDF as a dependency in the pom.xml file for installing this library in our application. Open your pom.xml file and add the following XML in the file.

  <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>2023.1.1</version>
    </dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.3</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Now, Build the project. The library will be automatically installed in our application from the maven repository.

Let's start with the simple example of creating a pdf file from an HTML string.

Create PDF file from HTML string:

Consider the following example:

 String htmlString = "<h1>My First PDF File<h1/><p> This is sample pdf file</p>";
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlString);

// Save the PdfDocument to a file
        try {
            myPdf.saveAs(Paths.get("myPDF.pdf") );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
Enter fullscreen mode Exit fullscreen mode

We have assigned our HTML content to a string variable. renderHtmlAsPdf() function provided by PdfDocument class of the IronPDF library takes a string as an argument and converts html content into a pdf document instance. The saveAs() function takes the location path as an argument and saves that pdf document instance into our provided directory.

The try-and-catch statement is used to handle any run time exception. It is just a best practice to use whenever dealing with files or databases.

Output:

The following is the pdf generated by the above code.
Image description

Convert HTML File into PDF File:

We can convert an HTML file into a pdf document.

The following is the sample HTML file that will be sued further in the example.
Rendered HTML
The following is the sample code snippet:

PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("myFile.html");

// Save the PdfDocument to a file
        try {
            myPdf.saveAs("myPDF.pdf");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
Enter fullscreen mode Exit fullscreen mode

The renderHtmlFileAsPdf() takes the HTML file path as an argument and converts the given HTML file into a pdf document. Later on, this pdf document is saved into a local drive using the saveAs() function.

Output:

The following is the generated pdf document by our program.
Image description
Now, we will use a large HTML document that has CSS and Javascript. We will observe the accuracy and design it maintains while converting html to pdf.

Convert HTML Files into a PDF document:

I will use the following Sample HTML page which has images, animation, styling, jQuery, and bootstrap.
Image description
Image description
We can see that the sample HTML document has images and complex styling. we will convert this html file into a pdf document and will observe the accuracy in terms of both styling and content.

We will use the same line of code as used in the above example.

 PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");

// Save the PdfDocument to a file
        try {
            myPdf.saveAs("myPDF.pdf");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
Enter fullscreen mode Exit fullscreen mode

The code explanation is already provided in the previous example. We have just changed the path, the rest is all same.

Output:

The following is the generated pdf:
Image description
We can see that generating pdf is very easy with IronPDF. The pdf content and style are the same as a source document.

We can also generate a pdf file using a URL.

Convert URL to PDF document:

The following code sample will generate a pdf file from a URL.

 PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");


// Save the PdfDocument to a file
        try {
            myPdf.saveAs("myPDF.pdf");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
Enter fullscreen mode Exit fullscreen mode

The renderUrlAsPdf() takes the URL as an argument and converts that URL into a pdf document. Later on, this pdf document is saved into a local drive using the saveAs() function.

Output:

The following is the generated pdf:
Image description
We can also add Watermark, header, footer, digital signature, and many more.

Let's add a custom watermark, header, and footer to our generated pdf.

Add Watermark:

The following code will add Watermark to our pdf file.
myPdf.applyWatermark("My Custom Watermark");

Add Header and Footer:

myPdf.addHtmlHeader(new HtmlHeaderFooter("my Header"));

Add Footer:

myPdf.addHtmlFooter(new HtmlHeaderFooter("my Header"));

Similarly, we can add foreground, copy pdf, merge pdf, and many more.

Summary:

In this tutorial, we have learned to convert HTML into pdf documents. We have generated a pdf file from an HTML string, HTML File, and URL. We have used simple to complex examples.

IronPDF made converting html to pdf very easy. We have converted html to pdf using just 1 line of code. It's fast, accurate, and more secure. There is the watermark of IronPDF in each generated pdf. It is because we are using free development. version. We can remove that by purchasing a 30-day free trial version or buying the license as per need.

I hope you have enjoyed the article. feel free to ask your queries in the comment section.

Top comments (0)