Java 库 PDF 生成 (完整代码示例)
本文将探讨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>
<!-- Add IronPDF dependency -->
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR-VERSION-HERE</version>
</dependency>
<!-- Add SLF4J logging dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>YOUR-VERSION-HERE</version>
</dependency>
</dependencies><dependencies>
<!-- Add IronPDF dependency -->
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR-VERSION-HERE</version>
</dependency>
<!-- Add SLF4J logging dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>YOUR-VERSION-HERE</version>
</dependency>
</dependencies>这将添加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"));
}
}- 第一步是使用
setLicenseKey方法应用许可证密钥。 密钥作为字符串参数传递; 在这种情况下,"YOUR-LICENSE-KEY"应替换为实际的许可证密钥。 - 下一步是使用
setLogPath方法设置日志路径。 这是保存IronPDF引擎日志文件的位置。 在这种情况下,它被设置为"C:/tmp/IronPdfEngine.log"。 - 定义主方法,并通过调用[
renderHtmlAsPdf](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String))方法创建一个[PdfDocument](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html)对象,传入HTML字符串作为参数。 这将把HTML转换为PDF并将其存储在myPdf对象中。 - 最后一步是使用[
saveAs](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#saveAs(java.lang.String))方法将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"));
}
}- [
PdfDocument.renderUrlAsPdf](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#renderUrlAsPdf(java.lang.String))方法专为此目的设计,接受包含要转换网页的URL的字符串。 该方法检索网页的HTML内容并将其转换为PDF文档。 IronPDF在保留所有网页组件外观的同时,使交互功能(链接、表单字段等)可操作。
结果如下:
从URL生成的输出PDF文件
摘要
总之,IronPDF是一个具有多种功能的Java库,用于创建和操作PDF文件。 无论您需要数字签署PDF文档,填写PDF表单,还是执行其他任务,IronPDF使这些操作变得简便,几乎无需编码。
凭借其可用的免费试用版和从$799起的灵活定价选项,IronPDF是为开发人员在项目中添加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文档为一个文件变得简单。










