使用 IRONPDF FOR JAVA

Java 庫 PDF 生成(完整代碼範例)

已更新 2024年9月1日
分享:

本文將探討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 檔案中:

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2025.1.1</version>
</dependency>

這將添加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  
        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"));
    }
}
JAVA

第一步是使用 setLicenseKey 方法申請許可證密鑰。 密鑰作為字串參數傳遞; 在這種情況下,應將 "YOUR-LICENSE-KEY" 替換為實際的授權金鑰。

下一步是使用 setLogPath 方法設定日誌路徑。 這是 IronPDF 引擎的日誌文件將被儲存的位置。 在此情況下,它設置為 "C:/tmp/IronPdfEngine.log"。

主方法已定義,並且一個PdfDocument物件是通过调用渲染Html為Pdf方法,將HTML字串作為參數傳入。 這將把 HTML 轉換為 PDF 並儲存在 myPdf 物件中。

最後一步是使用將myPdf物件保存到文件中。儲存為方法。 檔案位置作為參數以Paths物件的形式傳遞,在這個例子中是"HTMLtoPDF.pdf"。

在這裡,您可以看到上述程式的輸出,該程式使用IronPDF Java PDF函式庫創建了一個PDF文件。

Java 庫 PDF 生成(完整代碼範例),圖 1:來自 HTML 字串的輸出 PDF 檔案

從 HTML 字串生成的輸出 PDF 文件

從 URL 建立 PDF 檔案

IronPDF 可以將網頁渲染成 PDF包括來自本地網路和外部伺服器的各種來源。

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"));
JAVA

PdfDocument.renderUrlAsPdf方法專門為此目的設計,接受包含要轉換的網頁URL的字串。 該方法檢索網頁的 HTML 內容並將其轉換為 PDF 文件。 IronPDF 在生成互動功能的同時保留所有網頁組件的外觀。(連結、表單字段等。)功能性。

結果如下:

Java 庫 PDF 生成(完整程式碼範例),圖2:從 URL 輸出的 PDF 文件

從URL生成的輸出PDF文件

摘要

總結來說,IronPDF 是一個有價值的 Java 函式庫,具有許多用於創建和操作 PDF 文件的功能。 無論您是否需要數位簽署 PDF 文件, 填寫 PDF 表單,或執行其他任務,IronPDF 使您能以最少的編碼輕鬆完成。

With its免費試用可用並且靈活的價格選項從$749開始,IronPDF 是為開發人員尋求在其項目中添加 PDF 功能的經濟高效解決方案。

< 上一頁
HTML2PDF Java (代碼範例教程)
下一個 >
如何在 Java 中生成 PDF

使用 Maven 安裝

版本:2025.1.1

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2025.1.1</version>
</dependency>