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>
<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>
這將新增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/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)方法建立PdfDocument物件,傳入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"));
}
}
- [
PdfDocument.renderUrlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderUrlAsPdf(java.lang.String)方法專為此目的設計,接受包含要轉換的網頁URL的字串。 該方法檢索網頁的HTML內容並將其轉換為PDF文件。 IronPDF保留所有網頁組件的外觀,同時使互動功能(連結、表單字段等)可用。
結果如下:
從URL生成的PDF文件
摘要
總之,IronPDF是一個有價值的Java程式庫,具有許多用於建立和操作PDF文件的功能。 無論您需要數位簽署PDF文件、填寫PDF表單,或執行其他任務,IronPDF使這一切變得輕鬆,只需極少的程式碼。
借助其免費試用和靈活的價格選項,起價為$999,IronPDF對於希望在專案中新增PDF功能的開發者來說是一個具有成本效益的解決方案。
常見問題
如何在Java中建立PDF文件?
使用IronPDF,您可以在Java中利用其全面的API從頭生成新的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合併成一個文件。





