| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Full guide: How to Generate PDF Files Using Java
Automatically creating PDFs via Java programming greatly enhances the ability to produce documents dynamically, catering to needs such as generating invoices, reports, or other custom PDFs as required.
This guide details how to employ IronPDF for generating PDFs within Java-based software applications.
IronPDF offers a Java-based solution for crafting PDFs from HTML. Its user-friendly approach includes capabilities such as:
Beneficially, IronPDF integrates with the .NET Framework. This dual compatibility with .NET and Java expands its utility across different software environments.
Furthermore, IronPDF is not limited to PDF creation but also supports file conversions, text extraction from PDFs, and implementing password protections.
For using IronPDF within a Maven project, ensure the installation of:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>Begin by importing the IronPDF classes into your Java source file:
import com.ironsoftware.ironpdf.*;Next, activate IronPDF by inputting a valid license key in your main method:
License.setLicenseKey("Your license key");Note: License keys are necessary for watermark-free PDF creation. Purchase a License Key or Obtain a Free Trial License Key. Without a license key, PDFs will contain watermarks.
Convert HTML strings to PDFs using the renderHtmlAsPdf() method:
// Sample HTML content
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
// Transform HTML to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
// Save the new PDF
pdf.saveAs(Paths.get("html.pdf"));This generates a PDF named "html.pdf" from the provided HTML string content.
To create a PDF from an HTML file:
// Creating a PDF from an HTML file
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Storing the PDF
myPdf.saveAs(Paths.get("html_file_saved.pdf"));This method accurately renders the HTML and its associated styles.
Convert web pages to PDFs using the renderUrlAsPdf method:
// Convert web page to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the resulting PDF
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));Learn more about URL to PDF conversions on the Converting a URL to a PDF Tutorial page.
Adjust PDF formatting using the ChromePdfRenderOptions:
// Settings for PDF generation
ChromePdfRenderOptions options = new ChromePdfRenderOptions();
// Custom settings can be applied hereDetailed instructions for using this class can be found in the PDF Generation Settings Tutorial.
Create password-protected PDFs with the SecurityOptions class:
// Setting up security options
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
// Applying security to a PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Saving the secure PDF
urlToPdf.saveAs("protected.pdf");Protected PDFs will prompt for the password upon access:
For more on security and metadata, explore the Security and Metadata Tutorial.
Below is the full source code for this tutorial:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Insert license information
License.setLicenseKey("Your License Key");
// Create PDF from HTML string
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 PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));
// Apply password protection
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
urlToPdf.saveAs(Paths.get("protected.pdf"));
}
}IronPDF renders elements consistently with web standards, supporting interactive features within the PDF.
This guide examined the process of generating PDFs using Java with IronPDF. Its API creates PDFs from several kinds of content. Though it is a commercial product, you can start with a Free Trial.
| Back | FazBrowse Home | New Git URL |