跳過到頁腳內容
使用 IRONPDF FOR JAVA

Java Library 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>
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 "。
  • 定義了主要方法,並透過呼叫renderHtmlAsPdf 方法建立了PdfDocument對象,並將 HTML 字串作為參數傳遞。 這將把 HTML 轉換為 PDF 並將其儲存在myPdf物件中。 最後一步是使用[saveAs](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#saveAs(java.lang.String)方法將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;

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 保留了所有 Web 元件的外觀,同時使互動功能(連結、表單欄位等)能夠正常運作。

結果如下:

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

摘要

總之,IronPDF 是一個很有價值的 Java 程式庫,它提供了許多用於建立和操作 PDF 檔案的功能。 無論您需要對 PDF 文件進行數位簽名填寫 PDF 表單或執行其他任務,IronPDF 都能讓您輕鬆完成,而且只需極少的編碼。

IronPDF 提供免費試用版,定價方案靈活,起價僅為$799 ,對於希望在專案中添加 PDF 功能的開發人員來說,IronPDF 是一個經濟高效的解決方案。

常見問題解答

如何在 Java 中建立 PDF 文件?

使用 IronPDF for Java,您可以利用其全面的 API 從頭生成新的 PDF,或者將現有文件轉換為 PDF 格式,從而在 Java 中創建 PDF 文件。

在 Java 中將 HTML 轉換為 PDF 的流程是什麼?

要在 IronPDF for 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,您需要在 pom.xml 檔案中加入 IronPDF 相依性和 SLF4J 日誌相依性,然後執行 mvn install 指令。

如何使用 Java 操作現有的 PDF 檔案?

IronPdf 提供編輯文字、合併文件、新增註解和套用數位簽章的方法,可讓您處理現有的 PDF 檔案。

在購買之前,有沒有辦法測試 IronPdf 的功能?

是的,IronPDF 提供免費試用版,讓開發人員可以在做出購買決定之前測試其功能。

在 Java 中使用 PDF 函式庫有什麼好處?

在 Java 中使用 IronPDF for Java 這樣的 PDF 函式庫,可以簡化 PDF 的建立、編輯與轉換過程,減少大量編碼的需求並提高效率。

如何使用 Java 將多個 PDF 檔案合併為一個?

IronPDF 包含將多個 PDF 檔案合併為單一文件的功能,可輕鬆將多個 PDF 合併為單一文件。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。