Java PDF Converter (Code Example Tutorial)

In today's digital age, the need for converting documents to PDF format has become more essential than ever. The Portable Document Format, or PDF, is widely used across different industries because it ensures that the document will look the same on different devices and operating systems. PDF files are smaller in size compared to other file formats, which makes them easier to share and download.

In this article, we will discuss PDF conversion and how to convert files to PDF using Java. We will also introduce IronPDF, a Java PDF Library.

IronPDF: Java PDF Library

IronPDF is a Java PDF library that enables developers to create, edit, and manipulate PDF documents. It provides an easy-to-use API for converting HTML, CSS, and JavaScript to PDF documents, as well as a wide range of tools for customizing and modifying existing PDF files.

IronPDF offers excellent support for modern web technologies like CSS3 and JavaScript, making it possible to create PDF documents and pages that include advanced features such as interactive forms and embedded media.

Developers can use IronPDF's simple API to generate PDFs from scratch or convert existing HTML documents to PDFs. The library also offers a range of customization options, allowing developers to add headers, footers, watermarks, images, and other features to their PDF documents.

The library is designed to be fast and efficient, which means it can handle large PDF files and complex documents with ease. IronPDF also supports multi-threading, allowing developers to generate PDF documents in parallel and improve overall performance.

Getting Started with IronPDF

Installing IronPDF Java in a Maven project is a straightforward process. Follow the steps below to install the library using the provided dependency:

  1. Open your Maven project in an IDE such as Eclipse, IntelliJ IDEA, or NetBeans.
  2. Open the pom.xml file for your project.

Scroll down to the dependencies section and add the following code:

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

This code adds the IronPDF Java library and the slf4j-simple logging library as dependencies for your project.

  1. Save the pom.xml file.
  2. Build your project to download and install the IronPDF Java library and its dependencies.

You can now use the IronPDF Java library in your project. To do so, add the following import statement to your Java class:

import com.ironsoftware.pdf.*;
JAVA

You can now use the IronPDF for Java API to create, edit, and manipulate PDF documents.

You have successfully installed the IronPDF Java library in your Maven project using the provided dependency. You can now use the library to convert HTML documents to PDF, create PDF documents from scratch, and modify existing PDF documents.

Convert HTML to PDF file

To convert HTML to PDF using IronPDF for Java, you can use the following java code:

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class HtmlToPdfConverter {
    public static void main(String[] args) {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1> Made with IronPDF!");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

The above program sets the license key using the License.setLicenseKey method. It then sets the log path using the Settings.setLogPath method to specify the output folder where the log file should be saved.

Next, it uses the PdfDocument.renderHtmlAsPdf method to render the HTML as a PDF document. The HTML content is passed as a string argument to this method. In this example, the HTML content is a simple "Hello World" message.

After the PDF document is created, the program saves it to a file using the saveAs method, specifying the file path as an argument. In this example, the file is saved as "html_saved.pdf" in the current directory.

Convert HTML Files to PDF File Format

Using the HTML file to generate PDF files is a complex task. But IronPDF makes it very easy and with help of IronPDF, we can do this task with a few lines of code.

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("invoice.html");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
JAVA

First, the IronPDF library is imported along with the required Java IO classes. A license key is set to enable the use of the IronPDF library. The log path is set to the specified directory for logging purposes.

Next, the HTML file is rendered as a PDF document using the IronPDF renderHtmlFileAsPdf method. The resulting PDF document is stored in a variable named myPdf.

Finally, the PDF document is saved to a file using the saveAs method, with the output file path specified.

Output of PDF File

Output of PDF File

Convert URL to PDF File

The provided code can be used to convert any URL to a PDF document and save it to a file using the IronPDF library.

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
JAVA

Here are the steps involved in converting URL to PDF:

  1. Import the necessary classes from the IronPDF library and from the standard Java libraries.
  2. Set your IronPDF license key using the License.setLicenseKey method. You can obtain a license key from the IronPDF website.
  3. Set the path for the IronPDF log file using the Settings.setLogPath method. This step is optional but recommended for debugging purposes.
  4. Use the PdfDocument.renderUrlAsPdf method to render the URL as a PDF. This method returns a PdfDocument object that represents the generated PDF.
  5. Use the PdfDocument.saveAs method to save the PDF to a file. This method takes a Path object representing the file path where the PDF should be saved.
Output of PDF File

Output of PDF File

Summary

The article highlights the significance of using PDF format in today's digital age and introduces IronPDF as a Java PDF library that helps developers with creating, editing, and manipulating PDF documents. IronPDF is compatible with contemporary web technologies, such as CSS3 and JavaScript, and features a straightforward API that enables developers to generate PDF files from scratch or convert existing HTML documents to PDFs.

Moreover, the article provides practical examples to demonstrate how IronPDF can be used to convert HTML files, HTML content, and URLs into PDF documents. The pricing model for IronPDF's license starts at $749 and includes a free trial for developers to test the product before purchase.