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

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>
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

這裡您可以看到使用IronPDF Java PDF程式庫創建的PDF文件的顯示。

Java Library PDF Generation (Full Code Example), 圖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在保持網頁組件外觀的同時,還使交互功能(鏈接、表單欄位等)可正常運作。

結果如下:

Java Library PDF Generation (Full Code Example), 圖2:從URL輸出的PDF文件 從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 文件合併到一個文件中。

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

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

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我