在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
本文将探讨IronPDF库,这是一个用于在Java中创建PDF的出色工具。
IronPDF 是一款流行的 Java PDF 库 允许开发人员轻松创建PDF文档、PDF表单、数字签名PDF文件等。使用IronPDF,您可以使用现有的PDF文档作为 用于生成新PDF文件的模板将PDF数据存储在数据库中以备将来使用,将PDF转换为HTML等其他格式,甚至 合并多个PDF文件 合并成一个单一的文件。
IronPDF 允许用户 添加文本注释 为个性化他们创建的文件。此外,使用IronPDF,您可以在PDF中包括安全设置,如密码或水印。它有助于将PDF功能集成到Java程序中。IronPDF是一种极其多功能且强大的工具,能够快速且安全地生成PDF。让我们看看如何使用IronPDF创建PDF文件。
IronPDF 是一个用于创建 PDF 文件的宝贵工具。它具有您所需的所有功能,能够快速将文档、网页和图像转换为稳定、安全的 PDF 文件,从而轻松共享。让我们在这个演示程序中安装 IronPDF。
要在Maven项目中安装IronPDF Java,您可以将以下依赖项添加到项目的pom.xml
文件中:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2024.9.1</version>
</dependency>
这将添加 IronPDF for Java 库及其使用的 SLF4J 日志记录器。建议使用最新版本的 IronPDF for Java。添加完依赖项后,您可以运行 mvn install
将依赖项安装到本地版本库中,然后您的项目就可以使用 IronPDF for 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
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument
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>";
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);
//Save PDF document
myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
}
}
第一步是使用 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 文件
IronPDF 可以 渲染网页 从各种来源,包括本地网络和外部服务器获取数据。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
"(《世界人权宣言》) PdfDocument.renderUrlAsPdf
此方法专门为此设计,接受包含要转换的网页URL的字符串。该方法检索网页的HTML内容并将其转换为PDF文档。IronPDF保留所有网页组件的外观,同时使交互作用 (链接、表单字段等。) 功能。
结果如下:
从 URL 输出 PDF 文件
总而言之,IronPDF 是一个功能强大的 Java 库,具有许多用于创建和操作 PDF 文件的功能。无论您需要... 数字签署PDF, 填写表格,或执行其他任务,IronPDF使您能够通过最少的编码轻松地完成这些任务。
其 免费试用 灵活的定价选项从 $749 起,IronPDF 是开发人员在其项目中添加 PDF 功能的经济高效解决方案。