在 JAVA 中使用 IRONPDF Java 库 PDF 生成 (完整代码示例) Darrius Serrant 已更新:2025年6月22日 下载 IronPDF Maven 下载 JAR 下载 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 本文将探讨IronPDF库,它是一个用于在Java中创建PDF的优秀工具。 IronPDF:Java PDF库 IronPDF是一个流行的Java PDF库,允许开发人员轻松创建PDF文档、PDF表单、数字签署PDF文件等。 使用IronPDF,您可以使用现有的PDF文档作为模板生成新的PDF文件,将PDF数据存储在数据库中以备将来使用,将PDF转换为其他格式如HTML,甚至将多个PDF合并为一个。 IronPDF允许用户向PDF添加文本注释,以便个性化他们创建的文件。 此外,通过IronPDF,您可以在PDF中包含安全设置,如密码或水印。 它有助于将PDF功能集成到Java程序中。 IronPDF是一个多功能且强大的工具,可以快速安全地生成PDF。 让我们看看如何使用IronPDF创建PDF文件。 使用IronPDF生成PDF文件 IronPDF是创建PDF文件的无价工具。 它具备您需要的所有功能,可以快速将文档、网页和图像转换为稳定、安全的PDF,便于共享。 让我们在这个演示程序中安装IronPDF。 安装IronPDF Java PDF库 要在Maven项目中安装IronPDF Java,您可以将以下依赖项添加到项目的pom.xml文件中: <dependencies> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>YOUR-VERSION-HERE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>YOUR-VERSION-HERE</version> </dependency> </dependencies> <dependencies> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>YOUR-VERSION-HERE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>YOUR-VERSION-HERE</version> </dependency> </dependencies> XML 这将添加IronPDF for Java库和它使用的SLF4J日志记录器。 建议使用IronPDF for Java的最新版本。 添加依赖项后,您可以运行mvn install来安装本地库中的依赖项,您的项目将可以使用IronPDF for Java。 创建PDF文档的Java代码 这段代码是用Java编写的,使用了IronPDF库来将HTML转换为PDF文档。 // Import statement for IronPDF Java import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("YOUR-LICENSE-KEY"); // Set a log path to store log files generated by IronPDF Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Define the HTML content to convert into a PDF String html = "<!DOCTYPE html>\r\n" + "<html>\r\n" + " <head>\r\n" + " <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n" + " <style>\r\n" + " /* Add CSS styles for the invoice here */\r\n" + " body {\r\n" + " font-family: 'Popin', cursive;\r\n" + " }\r\n" + " .invoice {\r\n" + " width: 80%;\r\n" + " margin: 0 auto;\r\n" + " border: 1px solid #ccc;\r\n" + " padding: 20px;\r\n" + " background-color: #f5f5f5;\r\n" + " color: #333;\r\n" + " }\r\n" + " .invoice h1 {\r\n" + " text-align: center;\r\n" + " }\r\n" + " .invoice .invoice-info {\r\n" + " display: flex;\r\n" + " justify-content: space-between;\r\n" + " margin-bottom: 20px;\r\n" + " }\r\n" + " .invoice .invoice-info div {\r\n" + " width: 45%;\r\n" + " }\r\n" + " .invoice table {\r\n" + " width: 100%;\r\n" + " border-collapse: collapse;\r\n" + " }\r\n" + " .invoice table th, .invoice table td {\r\n" + " border: 1px solid #ccc;\r\n" + " padding: 10px;\r\n" + " }\r\n" + " .invoice table th {\r\n" + " text-align: left;\r\n" + " background-color: #f5f5f5;\r\n" + " }\r\n" + " .invoice table td {\r\n" + " text-align: right;\r\n" + " }\r\n" + " .invoice table td.total {\r\n" + " font-weight: bold;\r\n" + " }\r\n" + " </style>\r\n" + " </head>\r\n" + " <body>\r\n" + " <div class=\"invoice\">\r\n" + " <h1>Invoice</h1>\r\n" + " <div class=\"invoice-info\">\r\n" + " <div>\r\n" + " <p><strong>From:</strong></p>\r\n" + " <p>Your Company Name</p>\r\n" + " <p>123 Main St</p>\r\n" + " <p>City, State ZIP</p>\r\n" + " </div>\r\n" + " <div>\r\n" + " <p><strong>To:</strong></p>\r\n" + " <p>Customer Name</p>\r\n" + " <p>456 Park Ave</p>\r\n" + " <p>City, State ZIP</p>\r\n" + " </div>\r\n" + " </div>\r\n" + " <table>\r\n" + " <thead>\r\n" + " <tr>\r\n" + " <th>Product</th>\r\n" + " <th>Quantity</th>\r\n" + " <th>Price</th>\r\n" + " <th>Total</th>\r\n" + " </tr>\r\n" + " </thead>\r\n" + " <tbody>\r\n" + " <tr>\r\n" + " <td>Product 1</td>\r\n" + " <td>1</td>\r\n" + " <td>$10.00</td>\r\n" + " <td>$10.00</td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>Product 2</td>\r\n" + " <td>2</td>\r\n" + " <td>$5.00</td>\r\n" + " <td>$10.00</td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td colspan=\"3\" class=\"total\">Total:</td>\r\n" + " <td class=\"total\">$20.00</td>\r\n" + " </tr>\r\n" + " </tbody>\r\n" + " </table>\r\n" + " </div>\r\n" + " </body>\r\n" + "</html>"; // Convert HTML to PDF document PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html); // Save the PDF document to a specified path myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf")); } } // Import statement for IronPDF Java import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("YOUR-LICENSE-KEY"); // Set a log path to store log files generated by IronPDF Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Define the HTML content to convert into a PDF String html = "<!DOCTYPE html>\r\n" + "<html>\r\n" + " <head>\r\n" + " <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n" + " <style>\r\n" + " /* Add CSS styles for the invoice here */\r\n" + " body {\r\n" + " font-family: 'Popin', cursive;\r\n" + " }\r\n" + " .invoice {\r\n" + " width: 80%;\r\n" + " margin: 0 auto;\r\n" + " border: 1px solid #ccc;\r\n" + " padding: 20px;\r\n" + " background-color: #f5f5f5;\r\n" + " color: #333;\r\n" + " }\r\n" + " .invoice h1 {\r\n" + " text-align: center;\r\n" + " }\r\n" + " .invoice .invoice-info {\r\n" + " display: flex;\r\n" + " justify-content: space-between;\r\n" + " margin-bottom: 20px;\r\n" + " }\r\n" + " .invoice .invoice-info div {\r\n" + " width: 45%;\r\n" + " }\r\n" + " .invoice table {\r\n" + " width: 100%;\r\n" + " border-collapse: collapse;\r\n" + " }\r\n" + " .invoice table th, .invoice table td {\r\n" + " border: 1px solid #ccc;\r\n" + " padding: 10px;\r\n" + " }\r\n" + " .invoice table th {\r\n" + " text-align: left;\r\n" + " background-color: #f5f5f5;\r\n" + " }\r\n" + " .invoice table td {\r\n" + " text-align: right;\r\n" + " }\r\n" + " .invoice table td.total {\r\n" + " font-weight: bold;\r\n" + " }\r\n" + " </style>\r\n" + " </head>\r\n" + " <body>\r\n" + " <div class=\"invoice\">\r\n" + " <h1>Invoice</h1>\r\n" + " <div class=\"invoice-info\">\r\n" + " <div>\r\n" + " <p><strong>From:</strong></p>\r\n" + " <p>Your Company Name</p>\r\n" + " <p>123 Main St</p>\r\n" + " <p>City, State ZIP</p>\r\n" + " </div>\r\n" + " <div>\r\n" + " <p><strong>To:</strong></p>\r\n" + " <p>Customer Name</p>\r\n" + " <p>456 Park Ave</p>\r\n" + " <p>City, State ZIP</p>\r\n" + " </div>\r\n" + " </div>\r\n" + " <table>\r\n" + " <thead>\r\n" + " <tr>\r\n" + " <th>Product</th>\r\n" + " <th>Quantity</th>\r\n" + " <th>Price</th>\r\n" + " <th>Total</th>\r\n" + " </tr>\r\n" + " </thead>\r\n" + " <tbody>\r\n" + " <tr>\r\n" + " <td>Product 1</td>\r\n" + " <td>1</td>\r\n" + " <td>$10.00</td>\r\n" + " <td>$10.00</td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td>Product 2</td>\r\n" + " <td>2</td>\r\n" + " <td>$5.00</td>\r\n" + " <td>$10.00</td>\r\n" + " </tr>\r\n" + " <tr>\r\n" + " <td colspan=\"3\" class=\"total\">Total:</td>\r\n" + " <td class=\"total\">$20.00</td>\r\n" + " </tr>\r\n" + " </tbody>\r\n" + " </table>\r\n" + " </div>\r\n" + " </body>\r\n" + "</html>"; // Convert HTML to PDF document PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html); // Save the PDF document to a specified path myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf")); } } JAVA 第一步是使用setLicenseKey方法应用许可证密钥。 密钥作为字符串参数传递; 在这种情况下,"YOUR-LICENSE-KEY"应替换为实际的许可证密钥。 下一步是使用setLogPath方法设置日志路径。 这是保存IronPDF引擎日志文件的位置。 在这种情况下,它被设置为"C:/tmp/IronPdfEngine.log"。 主方法已经定义,并通过调用PdfDocument对象创建,通过调用renderHtmlAsPdf方法,传递HTML字符串作为参数。 这将把HTML转换为PDF,并存储在myPdf对象中。 最后一步是使用myPdf对象保存到文件。 文件位置作为Paths对象的参数传递,在这种情况下为"HTMLtoPDF.pdf"。 在这里您可以看到上述程序的输出,其中使用IronPDF Java PDF库创建了一个PDF文件。 从HTML字符串生成的输出PDF文件 从URL创建PDF文件 IronPDF可以将网页渲染为PDF,来源包括本地网络和外部服务器。 import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class UrlToPdfExample { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("YOUR-LICENSE-KEY"); // Set a log path to store log files generated by IronPDF Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Convert a webpage to a PDF by specifying the URL PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com"); // Save the PdfDocument to a file myPdf.saveAs(Paths.get("url.pdf")); } } import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class UrlToPdfExample { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("YOUR-LICENSE-KEY"); // Set a log path to store log files generated by IronPDF Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Convert a webpage to a PDF by specifying the URL PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com"); // Save the PdfDocument to a file myPdf.saveAs(Paths.get("url.pdf")); } } JAVA PdfDocument.renderUrlAsPdf方法专门为此目的设计,接受包含要转换的网页URL的字符串。 该方法检索网页的HTML内容并将其转换为PDF文档。 IronPDF在保留所有网页组件外观的同时,使交互功能(链接、表单字段等)可操作。 结果如下: 从URL生成的输出PDF文件 摘要 总之,IronPDF是一个具有多种功能的Java库,用于创建和操作PDF文件。 无论您需要数字签署PDF文档,填写PDF表单,还是执行其他任务,IronPDF使这些操作变得简便,几乎无需编码。 IronPDF提供免费试用和起始于$799的灵活定价方案,是开发者希望为其项目添加PDF功能的经济之选。 常见问题解答 如何在Java中创建PDF文档? 使用IronPDF,您可以利用其全面的API在Java中从头生成新的PDF或将现有文档转换为PDF格式来创建PDF文档。 在Java中将HTML转换为PDF的过程是什么? 为了在Java中将HTML转换为PDF,IronPDF提供了renderHtmlAsPdf方法,允许您输入HTML字符串并接收PDF文档作为输出。 如何在Java应用程序中将网页URL转换为PDF? IronPDF允许使用renderUrlAsPdf方法将网页URL转换为PDF。该方法从URL中检索HTML内容并将其转换为PDF文档。 可以使用Java库对PDF文档进行数字签名吗? 可以,IronPDF提供了对PDF文档进行数字签名的能力,确保文档的真实性和完整性。 如何使用Java为PDF添加安全功能? IronPDF提供了例如密码保护和水印的安全功能,可以应用于PDF以增强其安全性。 安装Maven项目中的PDF库涉及哪些步骤? 要在Maven项目中安装IronPDF,您需要将IronPDF依赖项和SLF4J日志依赖项添加到您的pom.xml文件中,然后执行mvn install命令。 如何使用Java操作现有的PDF文件? IronPDF允许您通过提供编辑文字、合并文件、添加注释和应用数字签名的方法来操作现有的PDF文件。 有没有办法在购买前测试IronPDF的功能? 有,IronPDF提供免费试用,允许开发人员在做出购买决策前测试其功能。 在Java中使用PDF库有哪些好处? 在Java中使用像IronPDF这样的PDF库简化了创建、编辑和转换PDF的过程,减少了大量的编码需求并提高了效率。 如何使用Java将多个PDF文件合并为一个文件? IronPDF包含将多个PDF文件合并为单个文档的功能,使合并多个PDF文档为一个文件变得简单。 Darrius Serrant 立即与工程团队聊天 全栈软件工程师(WebOps) Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。 相关文章 已更新2026年1月18日 如何在 Java 中将 TIFF 转换为 PDF 本完整指南将引导您在 Java 中使用 IronPDF 无缝地将 TIFF 图像转换为 PDF。 阅读更多 已更新2025年7月28日 如何在 Java 中将 PDF 转换为 PDFA 在本文中,我们将探讨如何在 Java 中使用 IronPDF 将 PDF 文件转换为 PDF/A 格式。 阅读更多 已更新2025年7月28日 如何在 Java 中创建 PDF 文档 本文将提供一个关于在 Java 中处理 PDF 的全面指南,涵盖关键概念、最佳库和示例。 阅读更多 HTML2PDF Java (代码示例教程)如何在 Java 中生成 PDF