How to Create PDF Files in Java
Creating PDF files in Java is simple using IronPDF library, which converts HTML to PDF with methods like renderHtmlAsPdf() for HTML strings, renderHtmlFileAsPdf() for HTML files, and renderUrlAsPdf() for web pages.
Quickstart: Create Your First PDF in Java
Add IronPDF dependency to your pom.xml:
<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>1.0.0</version> </dependency><dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>1.0.0</version> </dependency>XMLImport IronPDF classes:
import com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.*;JAVA- Create PDF from HTML: ```java :title=Quickstart PdfDocument pdf = PdfDocument.renderHtmlAsPdf(""); pdf.saveAs(Paths.get("output.pdf"));
How to Create PDF File in Java
- Install IronPDF Java Library
- Create PDF file from HTML string with
renderHtmlAsPdfmethod - Use
renderHtmlFileAsPdfmethod to create PDF File from HTML file - Create PDF from URL using
renderUrlAsPdfmethod - Export password protected PDF Files to desired directory
Creating PDFs programmatically in Java enables automated document generation for invoices, reports, and other business documents on demand.
This guide covers using IronPDF to create PDF files programmatically in Java applications.
What is IronPDF Java PDF Library?
IronPDF is a Java library for creating PDF documents from HTML. It provides functions for creating and customizing PDFs, including:
- Adding text, images, and other content types
- Choosing fonts, colors, and controlling layout and formatting
IronPDF is built on the .NET Framework, enabling use in both .NET and Java applications. The library supports advanced features like custom watermarks, PDF compression, and form creation.
IronPDF also handles PDF-related tasks including file format conversion, text and data extraction, and password encryption. You can merge multiple PDFs or split them as needed.
How Do I Create PDF Documents in a Java Application?
What Prerequisites Do I Need?
To use IronPDF in a Maven project, ensure these prerequisites are installed:
- Java Development Kit (JDK): Required for compiling and running Java applications. Download from Oracle website.
- Maven: Required for downloading project libraries. Download from Apache Maven website.
- IronPDF Library: Add to Maven project as dependency in pom.xml:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>For Gradle projects, add IronPDF using:
implementation 'com.ironsoftware:ironpdf:1.0.0'What Steps Should I Take Before Writing Code?
First, add this import statement to your Java source file:
import com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.*;For specific functionality, import additional classes:
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;Next, configure IronPDF with a valid license key in the main method:
License.setLicenseKey("Your license key");License.setLicenseKey("Your license key");Note: License keys remove watermarks. Purchase a License Key or Get a Free Trial. Without a license, PDFs generate with watermarks. See using license keys for details.
How Do I Create PDF Files from HTML String in Java?
Use renderHtmlAsPdf() to convert HTML strings to PDFs. This method supports HTML5, CSS3, and JavaScript rendering.
Pass an HTML string to renderHtmlAsPdf. IronPDF converts it to a PdfDocument instance:
// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));This creates "html.pdf" containing the HTML content.
Include complex HTML with CSS styling:
// HTML with CSS styling
String styledHtml = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #2563eb; }
.content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
</style>
</head>
<body>
<div class="content">
<h1>Styled PDF Document</h1>
<p>This PDF was created from HTML with custom CSS styling.</p>
</div>
</body>
</html>
""";
PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));// HTML with CSS styling
String styledHtml = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #2563eb; }
.content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
</style>
</head>
<body>
<div class="content">
<h1>Styled PDF Document</h1>
<p>This PDF was created from HTML with custom CSS styling.</p>
</div>
</body>
</html>
""";
PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));For advanced conversions, see the HTML to PDF tutorial.
How Do I Create PDF Files from HTML Pages in Java?
Create PDFs from local HTML files:
// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));The renderHtmlFileAsPdf method accepts a file path as a String or Path object.
IronPDF renders HTML elements with CSS and JavaScript exactly as browsers do. This includes external CSS files, images, and JavaScript libraries. See HTML files to PDF for details.
Use saveAs to save the PDF to a specific location.
How Do I Create PDF Files from URL in Java?
Use renderUrlAsPdf() to create PDFs from web pages:
// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");For authenticated websites, provide credentials:
// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");
// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");
// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));See URL to PDF Code Example for details. For complex authentication, see Website & System Logins.
How Do I Format PDF Files?
Use ChromePdfRenderOptions to specify PDF formatting. Configure page orientation, size, and margins. Pass the options as the second argument to render methods:
// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);
// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);
// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);
// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);
// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);
// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);
// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));See PDF Generation Settings for more options. Explore custom paper sizes and custom margins.
How Do I Password-Protect PDF Files?
Use SecurityOptions to password-protect PDFs:
// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");Set advanced security options:
// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");
// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");
// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);Apply security via the PDF's SecurityManager:
// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");Opening the PDF triggers a password prompt:
After entering the correct password, the PDF opens normally.
See Security and Metadata Example for additional settings.
What is the Complete Source Code?
Complete source code for this tutorial:
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("Your License Key");
// Convert HTML string to a PDF and save it
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
pdf.saveAs(Paths.get("html.pdf"));
// Convert HTML file to a PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert URL to a PDF and save it
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));
// Password-protect the PDF file
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
urlToPdf.saveAs(Paths.get("protected.pdf"));
}
}// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("Your License Key");
// Convert HTML string to a PDF and save it
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
pdf.saveAs(Paths.get("html.pdf"));
// Convert HTML file to a PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert URL to a PDF and save it
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));
// Password-protect the PDF file
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
urlToPdf.saveAs(Paths.get("protected.pdf"));
}
}IronPDF renders images and text without formatting loss. Buttons remain clickable, text boxes stay editable. The library supports adding signatures, rendering charts, and printing PDFs.
What Are the Key Takeaways?
This guide demonstrated creating PDFs in Java using IronPDF. IronPDF provides a simple API for generating PDFs from HTML files, XML documents, or other sources.
Generate reports, invoices, or any document type quickly. Deploy on AWS, Azure, or Google Cloud.
IronPDF requires a commercial license starting at $799. Get a Free Trial for production testing.
Frequently Asked Questions
What is the easiest way to create a PDF file in Java?
The easiest way to create a PDF in Java is using IronPDF's renderHtmlAsPdf() method. Simply pass an HTML string to this method and save the result: PdfDocument pdf = PdfDocument.renderHtmlAsPdf("Hello World!"); pdf.saveAs(Paths.get("output.pdf"));
How do I add IronPDF to my Maven project?
Add IronPDF to your Maven project by including this dependency in your pom.xml file:
Can I convert existing HTML files to PDF format?
Yes, IronPDF provides the renderHtmlFileAsPdf() method specifically for converting HTML files to PDF. This method reads an HTML file from your file system and converts it into a PDF document.
How can I generate PDFs from web pages in Java?
IronPDF offers the renderUrlAsPdf() method to convert web pages directly to PDF. Simply provide the URL of the webpage you want to convert, and IronPDF will render it as a PDF document.
What types of business documents can I create programmatically?
IronPDF enables automated generation of various business documents including invoices, reports, forms, and other on-demand documents. The library supports advanced features like custom watermarks, PDF compression, and form creation.
Is it possible to create password-protected PDFs?
Yes, IronPDF supports password encryption for PDF files. You can export password-protected PDFs to your desired directory, ensuring document security for sensitive business information.
What are the system requirements for using IronPDF in Java?
To use IronPDF in Java, you need the Java Development Kit (JDK) for compiling and running applications, Maven or Gradle for dependency management, and the IronPDF library added as a dependency to your project.
Can I manipulate existing PDFs, not just create new ones?
Yes, IronPDF handles various PDF manipulation tasks beyond creation. You can merge multiple PDFs, split them, extract text and data, and perform file format conversions using the library's comprehensive features.









