USING IRONPDF FOR JAVA

How to Generate PDF Report in Java

Updated September 15, 2024
Share:

This article will use IronPDF to create PDF reports. This can save a significant amount of time and effort compared to manually creating reports from scratch. Overall, a PDF report generator is an efficient and effective tool for creating professional-grade reports quickly and easily.

1. IronPDF for Java PDF library

IronPDF for Java PDF Library is a Java PDF library that allows developers to easily create, manipulate, and render PDF documents in their Java applications. With IronPDF, developers can generate high-quality PDFs from HTML, CSS, and JavaScript, as well as from other file formats such as images, Word documents, and Excel spreadsheets.

IronPDF is designed with simplicity and flexibility in mind, making it easy for developers to integrate PDF functionality into their Java applications. It provides a rich set of features, including support for adding text, images, and other media to PDF files, as well as the ability to add digital signatures, watermarks, and other security features. Additionally, IronPDF provides advanced PDF manipulation capabilities such as merging, splitting, and extracting pages from existing PDF documents.

2. Prerequisites

In order to generate a PDF report using IronPDF, there are certain prerequisites that need to be met. These prerequisites include:

  1. Java must be installed on your system, and its path must be set in the environment variables. If you haven't installed Java yet, you can download and install it from the official Java download page.
  2. An Integrated Development Environment (IDE) such as Eclipse or IntelliJ is required to write and execute your code.
  3. Maven, a build automation tool, should be integrated with your IDE to manage dependencies and build the project. If you're not familiar with Maven or need help integrating it with your IDE, you can refer to the official Maven integration guide for guidance.

Once these prerequisites are met, you can proceed with setting up your project and using IronPDF to generate PDF in your Java application.

3. IronPDF for Java Installation

To get started, open JetBrains IntelliJ IDEA and create a new Maven Java project.

How to Generate PDF Report in Java, Figure 1: Create a Maven project Create a Maven project

Upon starting a new project in JetBrains IntelliJ IDEA, a prompt will appear in a new window, asking for the project's name and location. After entering a suitable name and location, you may proceed by clicking the "Finish" button.

How to Generate PDF Report in Java, Figure 2: Configure the project Configure the project

Once you have clicked the "Finish" button, a new project will be opened in JetBrains IntelliJ IDEA, and you will be presented with the pom.xml file. This file will be used to add dependencies required for the Maven project.

How to Generate PDF Report in Java, Figure 3: The pom.xml file The pom.xml file

Add the following dependencies in the pom.xml file.

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.11.4</version>
</dependency>

After you have successfully added the above dependencies to the pom.xml file, you will notice a small icon appears in the top right corner of the file.

How to Generate PDF Report in Java, Figure 4: Install dependencies from the pom.xml file Install dependencies from the pom.xml file

Simply click on this icon to install dependencies. This process should only take a few minutes, depending on the speed of your internet connection. After that, create a new Java file or Java class files and start writing your code.

4. Create PDF Reports using IronPDF

With IronPDF, developers can quickly and easily create customized reports from a wide range of data sources, including databases, APIs, and other sources. One of the key benefits of using IronPDF for report generation is its ability to generate PDF reports from HTML. This means that developers can use familiar HTML and CSS markup to design their reports.

4.1. Create PDF Report from HTML

You can directly create a PDF report from HTML and save as PDF files at the same time using the renderHtmlAsPdf method. This method takes HTML code as input and generates a PDF instance as output. It uses the saveAs method to save data as a new document at a specified location.

The source code is given below:

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class main {
    public static void main(String[] args) throws IOException {
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("\n" +
                "\n" +
                "\n" +
                "\tSample Report\n" +
                "\t\n" +
                "\t\tbody {\n" +
                "\t\t\tfont-family: Arial, sans-serif;\n" +
                "\t\t\tfont-size: 14px;\n" +
                "\t\t}\n" +
                "\t\ttable {\n" +
                "\t\t\tborder-collapse: collapse;\n" +
                "\t\t\twidth: 100%;\n" +
                "\t\t}\n" +
                "\t\tth, td {\n" +
                "\t\t\tpadding: 8px;\n" +
                "\t\t\ttext-align: left;\n" +
                "\t\t\tborder-bottom: 1px solid #ddd;\n" +
                "\t\t}\n" +
                "\t\tth {\n" +
                "\t\t\tbackground-color: #f2f2f2;\n" +
                "\t\t}\n" +
                "\t\th1 {\n" +
                "\t\t\tfont-size: 24px;\n" +
                "\t\t\tmargin-bottom: 16px;\n" +
                "\t\t}\n" +
                "\t\tp {\n" +
                "\t\t\tmargin-bottom: 8px;\n" +
                "\t\t}\n" +
                "\t\n" +
                "\n" +
                "\n" +
                "\tSample Report\n" +
                "\tThis is a sample report generated with HTML.\n" +
                "\t\n" +
                "\t\t\n" +
                "\t\t\t\n" +
                "\t\t\t\tID\n" +
                "\t\t\t\tName\n" +
                "\t\t\t\tAge\n" +
                "\t\t\t\tEmail\n" +
                "\t\t\t\n" +
                "\t\t\n" +
                "\t\t\n" +
                "\t\t\t\n" +
                "\t\t\t\t1\n" +
                "\t\t\t\tJohn Doe\n" +
                "\t\t\t\t30\n" +
                "\t\t\t\tjohn.doe@example.com\n" +
                "\t\t\t\n" +
                "\t\t\t\n" +
                "\t\t\t\t2\n" +
                "\t\t\t\tJane Smith\n" +
                "\t\t\t\t25\n" +
                "\t\t\t\tjane.smith@example.com\n" +
                "\t\t\t\n" +
                "\t\t\t\n" +
                "\t\t\t\t3\n" +
                "Mike Johnson\n" +
                "40\n" +
                "mike.johnson@example.com\n" +
                "\n" +
                "\n" +
                "\n" +
                "" +
                "");

        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

Just simply write HTML code directly in your project, and it will generate a PDF file from it in an instant.

How to Generate PDF Report in Java, Figure 5: The output PDF file The output PDF file

4.2. Create PDF Documents Report from RTF Code

Using IronPDF, you can also render RTF code using a Java program. Below is the sample code for converting RTF into a PDF report. This example will use the renderRtfAsPdf method to render RTF code as a PDF instance and in the end use the saveAs method to save PDFs.

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class main {
    public static void main(String[] args) throws IOException {
        PdfDocument myPdf = PdfDocument.renderRtfAsPdf("{\\rtf1\\ansi\\deff0\n" +
                "{\\fonttbl{\\f0 Arial;}}\n" +
                "{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\n" +
                "\\pard\\plain\\fs24\\cf1\n" +
                "\\cbpat8\n" +
                "\\qc\\ul\\i\\cf2\\expndtw2\\charscalex120 Sample Report\\line\n" +
                "\\ulnone\\i0\\cf1\\charscalex100\\expndtw0\\par\n" +
                "\\pard\\plain\\fs18\\cf1\n" +
                "This is a sample report generated with RTF.\\par\n" +
                "\\par\n" +
                "\\pard\\plain\\fs16\\cf1\n" +
                "\\cbpat0\n" +
                "\\qc\n" +
                "{\\b Table 1: Sample Data}\\par\n" +
                "\\par\n" +
                "{\\pard\\plain\\fs16\\cf1\n" +
                "\\qc\n" +
                "{\\*\\shppict{\\pict\\pngblip\\picw98\\pich36\\picwgoal441\\pichgoal162\\hex5b5d5c5f5d5c5d5e5c5e5f5d5c5e5c5f5c5f5d5d5c5f5e5e5d5e5d5d5f5c5e5e5c5e5d5d5c5e5c5f5e5f5d5d5c5e5c5f5e5d5c5f5d5e5d5d5c5f5e5e5d5d5c5f5d5e5c5f5d5c5f5d5c5e5e5d5c5e5f5d5e5e5d5c5e5d5c5f5d5c5e5c5f5e5d5d5c5e5c5f5e5f5d5d5c5e5c5f5e5d5c5f5d5e5d5d5c5f5e5e5d5d5c5f5d5e5c5f5d5c5f5d5c5e5e5d5c5e5f5d5e5e5d5c5e5d5c5f5d5c5e5c5f5e5d5d5c5e5c5f5e5f5d5d5c5e5c5f5e5d5c5f5d5e5d5d5c5f5e5e5d5d5c5f5d5e5c5f5d5c5f5d5c5e5e5d5c5e5f5d5e5e5d5c5e5d5c5f5d5c5e5c5f5e5d5d5c5e5c5f5e5f5d5d5c5e5c5f5e5d5c5f5d5e5d5d5c5f5e5e5d5d5c5f5d5e5c5f5d5c5f5d5c5e5e5d5c5e5f5d5e5e5d5c5e5d5c5\n");

        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

How to Generate PDF Report in Java, Figure 6: The output PDF file from RTF code The output PDF file from RTF code

5. Conclusion

In conclusion, using a PDF report generator like IronPDF for Java PDF API can save businesses and organizations significant amounts of time and effort in creating professional reports.

For more tutorials and code examples, please visit the Java PDF creation examples. To know how to use IronPDF to generate PDFs, please visit the Java PDF creation tutorial.

IronPDF for Java is available for free for development purposes, but a commercial license is required for commercial use. However, you can get a free trial license for IronPDF to test IronPDF for Java's functionalities. For more information on licensing, please visit the IronPDF licensing guidelines.

< PREVIOUS
How to Parse PDFs in Java (Developer Tutorial)
NEXT >
How to Create PDF From Template in Java

Install with Maven

Version: 2024.11.4

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2024.11.4</version>
</dependency>

Ready to get started? Version: 2024.11 just released

Free Maven Download View Licenses >