如何設定IronPdfEngine

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPdfEngine是一個gRPC伺服器,能進行每一項PDF操作——建立、編輯、閱讀和渲染。 IronPDF for Java程式庫是此伺服器的API包裝器:當您的Java程式碼調用IronPDF的任何方法時,IronPdfEngine執行工作。 預設情況下,IronPDF for Java會將IronPdfEngine作為本地子進程生成,並自動管理其生命週期。 對於更高級的部署——共用微服務、Docker容器或氣隙網路,您可以將IronPdfEngine作為獨立的遠端伺服器運行。

快速開始:為Java設定IronPdfEngine

請注意每個版本的IronPDF for Java需要匹配的IronPdfEngine版本。不支持跨版本相容。 使用Settings.getIronPdfEngineVersion()確認所需版本。)]

您應該使用哪種部署模式?

IronPdfEngine支持兩種部署方式:本地和遠端。 在兩者之間的選擇取決於您的應用程式架構。

本地模式(預設)是獨立應用程式、桌面工具和單伺服器部署的正確選擇。 IronPDF for Java自動將IronPdfEngine作為子進程啟動——無需單獨的基礎設施。 在本地模式下,有兩種獲取IronPdfEngine二進制檔案的方式:在第一次運行時下載或將其捆綁為Maven相依項。

遠端模式適合於單個IronPdfEngine實例服務於多個應用節點的分佈式系統。 這也是基於Docker的部署、Kubernetes pods、無法在運行時下載二進制檔案的氣隙網路和將PDF處理集中化以減少每項服務開銷的任何情況的首選方法。

下面的部分詳細介紹了這兩種模式。

您如何設置本地IronPdfEngine?

有兩種選擇可以在本地執行IronPdfEngine。 兩者產生的運行時行為相同——差異在於何時及如何在目標機器上到達引擎二進制檔案。

選項1:在運行時下載IronPdfEngine

預設情況下,在新增了核心ironpdf相依項後,IronPDF會在第一次運行時檢測主機平臺(例如Windows x64或Linux x64),然後從網路下載匹配的IronPdfEngine二進制檔案。

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>20xx.xx.xx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>20xx.xx.xx</version>
</dependency>
XML

在初次下載後,二進制檔案本地快取,後續運行無需網路調用。

選項1——運行時下載:權衡
優點限制
最小的應用程式包大小 第一次運行需要存取網路
一個工件支持多個目標平臺 第一次運行啟動時較慢,因為要下載二進制檔案

選項2:將IronPdfEngine捆綁為相依項(推薦)

IronPDF Java提供平臺專用的Maven工件,將IronPdfEngine捆綁為一個.zip檔案於相依項內。 程式庫自動解壓並使用它——運行時無需網路調用。

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

然後為您的目標環境新增平臺工件。 只安裝您的目標平臺的工件——每一個都很大,安裝所有的會不必要地增加建構大小。

Windows x64

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

Windows x86

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x86</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x86</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

Linux x64

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

macOS x64

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

macOS ARM (Apple Silicon)

//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-arm64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
//:path=pom.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-arm64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

請注意ironpdf-engine-xxx-xxx相依項版本必須完全相同。 版本字串指的是IronPDF for Java的發布,不是IronPdfEngine的內部版本號。

選項2——捆綁相依項:權衡
優點限制
更快啟動——無需下載步驟 較大的應用程式包
安裝後無需存取網路 必須明確指定目標平臺
在氣隙環境中行為一致 安裝多個平臺工件會顯著增加建構大小

您如何連接到遠端IronPdfEngine?

遠端模式允許多個應用實例共享一個IronPdfEngine伺服器,通過gRPC。 這在微服務架構、容器化部署和PDF處理集中化的環境中很常見。

您如何驗證所需的IronPdfEngine版本?

版本匹配是嚴格的——IronPDF for Java 2024.2.2需要IronPdfEngine 2024.2.2精確匹配。 使用getIronPdfEngineVersion在部署伺服器之前確認所需的版本:

:path=/static-assets/ironpdf-java/content-code-examples/get-started/use-ironpdfengine-8.java
// Retrieve the IronPdfEngine version required by this Java library build
String ironPdfEngineVersion = Settings.getIronPdfEngineVersion();
System.out.println("Required IronPdfEngine version: " + ironPdfEngineVersion);
JAVA

部署或下載匹配返回版本字串的IronPdfEngine Docker鏡像或二進制檔案。

如何配置遠端連接?

假設IronPdfEngine正在123.456.7.8:33350上運行。 在調用任何IronPDF方法之前設置主機和端口。 最佳實踐是在應用程式啟動時進行這些調用,於任何PDF操作之前:

:path=/static-assets/ironpdf-java/content-code-examples/get-started/use-ironpdfengine-9.java
// Configure IronPDF for Java to connect to a remote IronPdfEngine instance
com.ironsoftware.ironpdf.Settings.setIronPdfEngineHost("123.456.7.8");
com.ironsoftware.ironpdf.Settings.setIronPdfEnginePort(33350);
JAVA

在這兩行執行後,應用中所有隨後的IronPDF調用會路由到遠端伺服器。 沒有本地IronPdfEngine子進程被啟動,您的pom.xml中不需要平臺專用的引擎相依項。

33350在之間的任何防火牆或安全群組中是開放的。

完整的遠端設置看起來如何?

以下範例連接到一個遠端IronPdfEngine實例並渲染一個HTML字串到PDF檔案——本地模式中使用的相同API,僅得出的配置不同:

//:path=Main.java
import com.ironsoftware.ironpdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Point the library to the remote IronPdfEngine server
        Settings.setIronPdfEngineHost("123.456.7.8");
        Settings.setIronPdfEnginePort(33350);

        // Render HTML to PDF — IronPdfEngine on the remote host performs the work
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello from remote IronPdfEngine</h1>");
        pdf.saveAs("output.pdf");
    }
}
//:path=Main.java
import com.ironsoftware.ironpdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Point the library to the remote IronPdfEngine server
        Settings.setIronPdfEngineHost("123.456.7.8");
        Settings.setIronPdfEnginePort(33350);

        // Render HTML to PDF — IronPdfEngine on the remote host performs the work
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello from remote IronPdfEngine</h1>");
        pdf.saveAs("output.pdf");
    }
}
JAVA

渲染的PDF透過gRPC返回到Java進程並且本地保存。 遠端伺服器處理所有基於Chrome的渲染、字體載入和資源解析。

接下來的步驟是什麼?

配置完IronPdfEngine後,整個IronPDF for Java功能集可用——HTML到PDF,PDF編輯、合併、加蓋等等。

常見問題

什麼是IronPdfEngine以及為什麼IronPDF for Java需要它?

IronPdfEngine是一個執行所有PDF操作的gRPC伺服器-建立,編輯,閱讀和渲染。IronPDF for Java 是這個伺服器的API封裝,因此每個Java方法呼叫都由IronPdfEngine處理。如果沒有它,Java程式庫無法執行任何PDF工作。

本地和遠程IronPdfEngine有什麼區別?

本地模式將IronPdfEngine作為在同一臺Java應用程式機器上的子進程啟動-不需要額外的基礎設施。遠程模式通過gRPC連接到獨立的IronPdfEngine伺服器,適合微服務,Docker 部署和共享PDF處理節點。

什麼時候應該使用遠程IronPdfEngine而不是本地?

當您需要多個應用程式實例共享一個PDF處理伺服器,在Kubernetes或Docker Compose環境中部署時,或在運行不允許在運行時下載二進位的氣隙網路中時,使用遠程IronPdfEngine。

如何在Java中配置遠程IronPdfEngine連接?

在應用程式啟動時,在調用任何IronPDF方法之前,使用 Settings.setIronPdfEngineHost() 設置伺服器IP或主機名,使用 Settings.setIronPdfEnginePort()設置埠號碼。預設的IronPdfEngine埠是33350

我應該使用哪個Maven工件來捆綁IronPdfEngine作為依賴項?

新增與您的部署目標匹配的特定平台的工件 — 例如,ironpdf-engine-linux-x64 用於Linux伺服器或ironpdf-engine-windows-x64 用於Windows。工件版本必須與ironpdf核心依賴版本完全匹配。

如何檢查我的Java程式庫需要哪個IronPdfEngine版本?

在您的Java程式碼中調用 Settings.getIronPdfEngineVersion()。它返回必須與您部署的IronPdfEngine二進位或Docker映像匹配的確切版本字串。

我可以在一個專案中安裝多個平台引擎依賴項嗎?

技術上是的,但每個工件都很大,安裝多個會不必要地使您的構建膨脹。僅安裝與目標部署平台匹配的工件。

我需要有互聯網連接才能使用IronPDF for Java嗎?

只有在您使用運行時下載選項(於pom.xml中無特定平台依賴項)時需如此。在第一次運行時,IronPDF會下載您平台的IronPdfEngine二進位。完成該下載後,不再需要互聯網存取。如新增捆綁的平面依賴項或使用遠程模式,任何時候都不需要互聯網存取。

How do I detect and recover from a dead IronPdfEngine in Java?

Call IronPdfEngineManager.isEngineActive() to check whether the engine is connected, then IronPdfEngineManager.restartEngine() to force a clean reconnect if it is not. This watchdog pattern lets long-running services recover automatically when the engine is interrupted by a host restart, an operating system kill, or a native crash. It is an advanced feature; most applications rely on the automatic lifecycle and never call it.

Can I restart IronPdfEngine when using a custom gRPC connection?

No. In CUSTOM connection mode (a channel passed to IronPdfEngineConnection.withCustomGrpcConnection), stopEngine() and restartEngine() throw UnsupportedOperationException because the caller owns the channel and IronPDF cannot rebuild it. isEngineActive() and startEngine() still work in every connection mode.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
版本: 2026.6 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想要快速證明嗎?
運行範例 觀看您的HTML變成PDF。