本指南涵蓋了在 Azure Functions 容器中部署 IronPDF for Java,並透過無伺服器 HTTP 端點按需生成 PDF 所需的所有內容。 由於 IronPDF 內建原生 Chromium 渲染引擎,因此必須以 Docker 映像檔形式打包 —— Azure Functions 的標準 ZIP 部署方式無法執行 IronPDF 運行時所依賴的二進位檔。遵循本指南,一個可運作的 Azure Function 將接受 URL 作為查詢參數,並回傳一個已完全渲染的 PDF 檔案供使用者下載。

此方法採用 Microsoft 建議的自訂容器工作流程,適用於基於 Linux 的 Azure Functions。 Maven 專案負責提供功能程式碼與依賴項管理。 Docker 會建立容器映像檔,該映像檔會推送至註冊表,並由 Azure Function App 進行引用。部署完成後,冷啟動時間是主要的效能考量因素——後續的呼叫則快速且穩定。

開始之前,請確認已在本機安裝 Azure CLI、Docker Desktop、Maven 3.8 以上版本,以及 JDK 11 或 JDK 17。 此外,還需具備有效的 Azure 訂閱,並擁有建立 Function Apps 及儲存帳戶的權限。

快速入門:在 Azure Functions 上部署 IronPDF for Java

以下程式碼展示了完整的 RenderPdf Azure Function。 它接受 url 查詢參數,並返回 PDF 位元組流。 請在完成後續章節中的 Maven 依賴項設定後,將此內容加入 Function.java 中。

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/RenderPdf.java
import com.microsoft.azure.functions.*;
import com.ironsoftware.ironpdf.PdfDocument;
import java.util.Optional;

public class Function {

    /**
     * HTTP-triggered Azure Function: accepts a URL, renders it as a PDF,
     * and returns the PDF bytes as a downloadable attachment.
     */
    @FunctionName("RenderPdf")
    public HttpResponseMessage renderPdf(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        context.getLogger().info("RenderPdf function triggered.");

        // Read the target URL from the query string
        final String url = request.getQueryParameters().get("url");

        if (url == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Provide a 'url' query parameter.")
                    .build();
        }

        try {
            context.getLogger().info("Rendering URL as PDF: " + url);

            // IronPDF renders the full page including JavaScript
            PdfDocument pdf = PdfDocument.renderUrlAsPdf(url);
            byte[] pdfBytes = pdf.getBinaryData();

            return request.createResponseBuilder(HttpStatus.OK)
                    .body(pdfBytes)
                    .header("Content-Disposition", "attachment; filename=output.pdf")
                    .header("Content-Type", "application/pdf")
                    .build();

        } catch (Exception ex) {
            context.getLogger().severe("PDF rendering failed: " + ex.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("PDF rendering failed. Check function logs for details.")
                    .build();
        }
    }
}
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/RenderPdf.java
import com.microsoft.azure.functions.*;
import com.ironsoftware.ironpdf.PdfDocument;
import java.util.Optional;

public class Function {

    /**
     * HTTP-triggered Azure Function: accepts a URL, renders it as a PDF,
     * and returns the PDF bytes as a downloadable attachment.
     */
    @FunctionName("RenderPdf")
    public HttpResponseMessage renderPdf(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        context.getLogger().info("RenderPdf function triggered.");

        // Read the target URL from the query string
        final String url = request.getQueryParameters().get("url");

        if (url == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Provide a 'url' query parameter.")
                    .build();
        }

        try {
            context.getLogger().info("Rendering URL as PDF: " + url);

            // IronPDF renders the full page including JavaScript
            PdfDocument pdf = PdfDocument.renderUrlAsPdf(url);
            byte[] pdfBytes = pdf.getBinaryData();

            return request.createResponseBuilder(HttpStatus.OK)
                    .body(pdfBytes)
                    .header("Content-Disposition", "attachment; filename=output.pdf")
                    .header("Content-Type", "application/pdf")
                    .build();

        } catch (Exception ex) {
            context.getLogger().severe("PDF rendering failed: " + ex.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("PDF rendering failed. Check function logs for details.")
                    .build();
        }
    }
}
JAVA

立即透過免費試用,在您的專案中開始使用 IronPDF。

第一步:
green arrow pointer

目錄

有哪些先決條件? {#prerequisites}

開始之前,請確認已安裝所有必要工具,且 Azure 訂閱處於有效狀態。若跳過這些檢查,往往會導致部署過程中途發生建置失敗。

所需本地化工具

所需的 Azure 資源

  • 有效的 Azure 訂閱
  • 建立資源群組、儲存帳戶及函式應用程式方案的權限
  • 一個 Docker Hub 帳戶(或 Azure Container Registry),用於託管已建置的映像檔

重要事項IronPDF for Java 在任何 Docker 容器內執行時,需要 ironpdf-engine-linux-x64 組件。 Azure Functions 的標準 ZIP 部署無法執行 IronPDF 的原生二進位檔 — Docker 是唯一受支援的部署方式。)}]

執行 az login 以驗證 Azure CLI,然後再繼續進行下一節。

如何設定 Azure Function 專案? {#set-up-project}

微軟關於在 Linux 上使用自訂映像建立函式的指南,涵蓋了完整的架構建置流程。 請依照上述步驟操作,並注意一項重要選擇:當系統提示選擇程式語言時,請選擇 Java

請按照指南逐步操作,直到架構好的專案成功建置,並能透過 Azure Functions Core Tools 在本地端執行佔位函式。 請透過以下方式進行驗證:

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/local-run.sh
mvn clean package
func start
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/local-run.sh
mvn clean package
func start
SHELL

一旦佔位符能回應本地 HTTP 請求,即表示專案結構正確無誤,已準備好進行 IronPDF 整合。 關鍵檔案為 pom.xml(Maven 配置檔)、Function.java(函式程式碼)以及 Dockerfile(容器定義檔)。

請注意Azure Functions Maven 範本會產生 host.jsonlocal.settings.json,並與 pom.xml 並列。 local.settings.json 檔案用於儲存本地開發的環境變數 — 該檔案預設不納入版本控制,且絕不應提交至版本控制系統。}}]

如何將 IronPDF 依賴項加入您的 Maven 專案? {#add-IronPDF-dependencies}

IronPDF for Java 透過 Maven Central 進行發行。 需提供兩項組件:提供 Java API 的核心 ironpdf 函式庫,以及 ironpdf-engine-linux-x64(此組件封裝了針對 Linux x86-64 編譯的原生 Chromium 引擎)。正是由於引擎組件的存在,才使得 Docker 部署成為必要條件——該組件包含必須在執行時運行的二進位檔。

開啟 pom.xml,並在 <dependencies> 區塊內加入以下內容。 請將 LATEST_VERSION 替換為 Maven Central 上可用的最新版本:

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/pom.xml
<dependencies>

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

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf-engine-linux-x64</artifactId>
        <version>LATEST_VERSION</version>
    </dependency>
</dependencies>
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/pom.xml
<dependencies>

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

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf-engine-linux-x64</artifactId>
        <version>LATEST_VERSION</version>
    </dependency>
</dependencies>
XML

兩項產品均須使用相同的版本號。 當函式首次嘗試渲染 PDF 時,ironpdfironpdf-engine-linux-x64 之間的版本不匹配會導致執行時異常。

更新 pom.xml 後,請執行 mvn dependency:resolve 以確認 Maven 能從 Central 下載這兩項工件,再投入時間建立 Docker 映像檔。

{t:(請參閱 [IronPDF for Java 發行說明](https://ironpdf.com/java/release-notes) 以獲取最新穩定版本的資訊。 使用最新版本可確保與最新的 Chromium 渲染引擎相容,並避免已知錯誤。)}]

如何撰寫 RenderPdf 函式? {#write-renderpdf-function}

RenderPdf 函式是一個由 HTTP 觸發的 Azure 函式,它接受 url 查詢參數,使用 IronPDF 基於 Chromium 的引擎渲染目標頁面,並將生成的 PDF 作為二進位回應,附帶 Content-Disposition: attachment 標頭返回。 此標頭會指示瀏覽器(或 HTTP 客戶端)下載 PDF 檔案,而非直接在網頁中顯示。

完整的函式程式碼請參閱上方的"快速入門"部分。 請將其置於 src/main/java/com/example/Function.java 中,以取代或擴充由 Maven 範本生成的佔位符。

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/RenderPdf-annotated.java
import com.microsoft.azure.functions.*;
import com.ironsoftware.ironpdf.PdfDocument;
import java.util.Optional;

public class Function {

    @FunctionName("RenderPdf")
    public HttpResponseMessage renderPdf(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        // Log each invocation for Azure Monitor / Application Insights
        context.getLogger().info("RenderPdf triggered.");

        final String url = request.getQueryParameters().get("url");

        // Return 400 if no URL was supplied
        if (url == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Provide a 'url' query parameter.")
                    .build();
        }

        try {
            // renderUrlAsPdf launches Chromium, loads the page, and captures it as PDF
            PdfDocument pdf = PdfDocument.renderUrlAsPdf(url);

            // getBinaryData returns the raw PDF bytes ready for transmission
            byte[] pdfBytes = pdf.getBinaryData();

            return request.createResponseBuilder(HttpStatus.OK)
                    .body(pdfBytes)
                    .header("Content-Disposition", "attachment; filename=output.pdf")
                    .header("Content-Type", "application/pdf")
                    .build();

        } catch (Exception ex) {
            context.getLogger().severe("Rendering error: " + ex.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("PDF rendering failed.")
                    .build();
        }
    }
}
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/RenderPdf-annotated.java
import com.microsoft.azure.functions.*;
import com.ironsoftware.ironpdf.PdfDocument;
import java.util.Optional;

public class Function {

    @FunctionName("RenderPdf")
    public HttpResponseMessage renderPdf(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        // Log each invocation for Azure Monitor / Application Insights
        context.getLogger().info("RenderPdf triggered.");

        final String url = request.getQueryParameters().get("url");

        // Return 400 if no URL was supplied
        if (url == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                    .body("Provide a 'url' query parameter.")
                    .build();
        }

        try {
            // renderUrlAsPdf launches Chromium, loads the page, and captures it as PDF
            PdfDocument pdf = PdfDocument.renderUrlAsPdf(url);

            // getBinaryData returns the raw PDF bytes ready for transmission
            byte[] pdfBytes = pdf.getBinaryData();

            return request.createResponseBuilder(HttpStatus.OK)
                    .body(pdfBytes)
                    .header("Content-Disposition", "attachment; filename=output.pdf")
                    .header("Content-Type", "application/pdf")
                    .build();

        } catch (Exception ex) {
            context.getLogger().severe("Rendering error: " + ex.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("PDF rendering failed.")
                    .build();
        }
    }
}
JAVA

PdfDocument.renderUrlAsPdf(url) 會在容器內啟動一個無頭 Chromium 實例,完全載入目標 URL(包含 JavaScript),並將渲染後的輸出擷取為 PDF 檔案。 這會產生與使用者在瀏覽器中所見完全相同的視覺輸出,因此非常適合擷取現代網頁應用程式、儀表板及報表頁面。

重要事項函式觸發器中的 authLevel = AuthorizationLevel.ANONYMOUS 設定會使端點對外公開。 若用於生產環境部署,請將此處改為 FUNCTIONADMIN,並在請求標頭中傳遞功能鍵。)}]

如何為 IronPDF 設定 Dockerfile? {#configure-dockerfile}

IronPDF 的 Chromium 引擎依賴一組共享的 Linux 函式庫,而這些函式庫並未包含在 Azure Functions 的基礎映像中。 基礎映像 mcr.microsoft.com/azure-functions/java:4-java17-build 建構於 Debian 11 之上,因此必須使用 apt 安裝套件。

必須將以下 RUN 指令加入由 Azure Functions Maven 範本所產生的 Dockerfile 檔案中。 請將它們置於 FROM 指令之後,並位於添加應用程式 JAR 的 COPY 步驟之前:

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/Dockerfile
FROM mcr.microsoft.com/azure-functions/java:4-java17-build AS installer-env

# Install system dependencies required by IronPDF's Chromium renderer
RUN apt-get update && apt-get install -y \
    libgdiplus \
    libxkbcommon-x11-0 \
    libc6 \
    libc6-dev \
    libgtk2.0-0 \
    libnss3 \
    libatk-bridge2.0-0 \
    libx11-xcb1 \
    libxcb-dri3-0 \
    libdrm-common \
    libgbm1 \
    libasound2 \
    libxrender1 \
    libfontconfig1 \
    libxshmfence1 \
    && apt-get install -y xvfb libva-dev libgdiplus \
    && rm -rf /var/lib/apt/lists/*

# Copy the built function JAR
COPY --from=installer-env /home/site/wwwroot /home/site/wwwroot

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

libgdiplus 套件提供用於圖形渲染的 GDI+ 相容性。 libnss3libatk-bridge2.0-0 是 Chromium 沙箱與無障礙層所必需的。 xvfb 提供一個虛擬幀緩衝區,即使在某些 Debian 配置的無頭模式下,Chromium 仍需此功能。 rm -rf /var/lib/apt/lists/* 區塊末端的 RUN 步驟會清除套件管理員的快取,以將最終映像檔大小壓縮至最小。

請注意若 Azure Functions 基礎映像版本變更,或採用其他 Linux 發行版作為基礎,所需套件可能會有所不同。 請參閱《IronPDF Linux 安裝指南》,以獲取涵蓋 Debian、Ubuntu、CentOS 及 Alpine 的完整依賴關係對照表。

如何建立並推送 Docker 映像檔? {#build-push-docker}

在完成 Maven 專案建置並更新 Dockerfile 後,即可組建容器映像檔並上傳至 Docker 註冊表。 當 Function App 建立或更新時,Azure Functions 會載入此圖片。

步驟 1 — 編譯並打包 Maven 專案

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/build.sh
# Compile the Java code and package it as a JAR
mvn clean package
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/build.sh
# Compile the Java code and package it as a JAR
mvn clean package
SHELL

Maven 會編譯函式碼、解析所有依賴項(包含兩項 IronPDF 組件),並在 target/ 目錄中產生可部署的 JAR 檔案。 請先修正所有編譯錯誤,再繼續進行。

步驟 2 — 建立 Docker 映像檔

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/docker-build.sh
# Replace <DOCKER_ID> with your Docker Hub username or ACR login server
docker build --tag <DOCKER_ID>/ironpdf-azure-functions:v1.0.0 .
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/docker-build.sh
# Replace <DOCKER_ID> with your Docker Hub username or ACR login server
docker build --tag <DOCKER_ID>/ironpdf-azure-functions:v1.0.0 .
SHELL

此建置程序會安裝 Dockerfile 中列出的 Linux 套件、複製 JAR 檔案,並將所有內容整合至最終映像檔中。 首次建置時,由於需下載套件並建立快取層,此過程可能需要數分鐘。 後續使用相同基礎映像的建置速度將顯著提升。

步驟 3 — 將映像檔推送至 Docker Hub

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/docker-push.sh
# Authenticate if not already logged in
docker login

# Push the image to the registry
docker push <DOCKER_ID>/ironpdf-azure-functions:v1.0.0
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/docker-push.sh
# Authenticate if not already logged in
docker login

# Push the image to the registry
docker push <DOCKER_ID>/ironpdf-azure-functions:v1.0.0
SHELL

提示Azure Container Registry (ACR) 是 Docker Hub 的私有替代方案。 ACR 可直接與 Azure Active Directory 整合,是影像隱私至關重要的生產環境工作負載的首選方案。

如何將此函式部署至 Azure? {#deploy-to-azure}

將映像檔上傳至登錄檔後,即可建立(或更新)Azure Function App 以引用該映像檔。 az functionapp create 指令可透過單一步驟完成 Function App 的配置、將其連結至儲存帳戶,並設定容器映像。

步驟 1 — 建立或更新 Function App

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/az-deploy.sh
az functionapp create \
  --name <APP_NAME> \
  --storage-account <STORAGE_NAME> \
  --resource-group AzureFunctionsContainers-rg \
  --plan myPremiumPlan \
  --deployment-container-image-name <DOCKER_ID>/ironpdf-azure-functions:v1.0.0
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/az-deploy.sh
az functionapp create \
  --name <APP_NAME> \
  --storage-account <STORAGE_NAME> \
  --resource-group AzureFunctionsContainers-rg \
  --plan myPremiumPlan \
  --deployment-container-image-name <DOCKER_ID>/ironpdf-azure-functions:v1.0.0
SHELL

請將 <APP_NAME> 替換為 Function App 的全域唯一名稱,<STORAGE_NAME> 替換為現有的 Azure Storage 帳戶名稱,並將 <DOCKER_ID> 替換為前一步驟中使用的 Docker Hub 使用者名稱或 ACR 登入伺服器。

--plan myPremiumPlan 標記代表選擇了 Premium 託管方案。 IronPDF 的 Chromium 引擎在渲染過程中會消耗大量記憶體; "基本方案"的 1.5 GB 記憶體上限通常不足。 Premium 方案提供至少 3.5 GB 儲存空間,並支援預熱實例,可消除冷啟動延遲。

步驟 2 — 驗證部署

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/az-verify.sh
# Check that the function app is running and the container has been pulled
az functionapp show \
  --name <APP_NAME> \
  --resource-group AzureFunctionsContainers-rg \
  --query "state"
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/az-verify.sh
# Check that the function app is running and the container has been pulled
az functionapp show \
  --name <APP_NAME> \
  --resource-group AzureFunctionsContainers-rg \
  --query "state"
SHELL

當容器成功啟動時,該指令會傳回 "Running"。 若回傳 "Starting" 或發生錯誤,請檢查 Azure 入口網站中"Function App"下的 Log Stream,以確認是否存在容器拉取或啟動錯誤。

警告不建議在 Azure Functions 上使用 IronPDF 的"用量 (無伺服器)"方案。 使用 Chromium 進行 PDF 渲染所需的記憶體,超過了"基本方案"所分配的配額。 請使用 Premium 或 Dedicated (App Service) 方案以避免發生記憶體不足的錯誤。)}]

如何觸發並測試此功能? {#trigger-and-test}

一旦 Function App 回報 Running 狀態,RenderPdf 端點即準備好接受請求。 端點 URL 遵循可預測的模式,其結構基於 Function App 名稱以及 @FunctionName 註解中定義的函式名稱。

請使用瀏覽器或 curl 進行測試

//:path=/static-assets/pdf/content-code-examples/tutorials/azure/test-request.sh
# Replace <APP_NAME> with the Function App name
curl -o output.pdf \
  "https://<APP_NAME>.azurewebsites.net/api/RenderPdf?url=https://www.example.com"
//:path=/static-assets/pdf/content-code-examples/tutorials/azure/test-request.sh
# Replace <APP_NAME> with the Function App name
curl -o output.pdf \
  "https://<APP_NAME>.azurewebsites.net/api/RenderPdf?url=https://www.example.com"
SHELL

成功的回應會將一個名為 output.pdf 的 PDF 檔案儲存至當前目錄。 curl 中的 -o 標誌會將二進位回應內容寫入檔案,而非 PRINT 至終端機。

在瀏覽器中測試時,請導航至:

https://<APP_NAME>.azurewebsites.net/api/RenderPdf?url=https://www.example.com

瀏覽器將提示您下載 PDF 檔案。 開啟它以確認頁面已正確渲染。

重要事項冷啟動後的首次請求可能需要 20–60 秒,因為 Azure 會拉取容器映像檔,且 IronPDF 會初始化 Chromium。 在同一個容器生命週期內,後續的請求速度會快得多。 Premium 方案的預熱實例功能透過持續運行至少一個實例,從而消除冷啟動問題。

檢查日誌中的錯誤:前往 Azure 入口網站,開啟 Function App,並在"監控"下選取"日誌串流"。 來自 context.getLogger() 呼叫的日誌記錄會在此近乎即時地顯示,這使得診斷渲染失敗的情況變得直觀簡單。

接下來該怎麼做? {#next-steps}

本指南示範了如何在 Azure Functions Docker 容器中部署 IronPDF for Java、編寫一個將 URL 渲染為 PDF 的 HTTP 觸發函式、使用必要的 Linux 依賴項配置 Dockerfile,以及測試實際運作的端點。 此模式可延伸應用於更進階的使用情境,僅需進行微幅調整。

擴展功能

  • 直接使用 PdfDocument.renderHtmlAsPdf(htmlString) 渲染 HTML 字串,而非使用 URL
  • 使用 IronPDF 的完整 Java PDF API 套用浮水印、合併多個 PDF 檔案,或添加數位簽章
  • 讀取請求標頭或 POST 內容,以傳遞自訂 HTML 內容或渲染選項

提升生產就緒性

  • authLevel 切換為 FUNCTION,並定期旋轉功能鍵
  • 使用 Azure Key Vault 儲存應用程式設定中引用的任何機密
  • 設定 Application Insights 以實現渲染延遲與失敗率的端到端可觀察性
  • 設定 Docker 映像更新 Webhook,以便當推送新映像版本時,Azure 會自動重新部署

探索更多 IronPDF for Java 指南

立即開始免費試用 IronPDF,在評估期間即可無水印地使用所有渲染與處理功能。 當準備部署至生產環境時,請查看 IronPDF 授權方案,以找到符合專案規模的方案。

常見問題

為何在 Azure Functions 上部署 IronPDF 需要使用 Docker?

IronPDF 內建原生 Chromium 渲染引擎,必須在執行時執行二進位檔。Azure Functions 的 ZIP 部署無法執行原生二進位檔,因此 Docker 容器映像檔是唯一受支援的部署途徑。

要在 Docker 容器中執行 IronPDF,需要哪些 Maven 組件?

pom.xml: com.ironsoftware:ironpdf Java API 及 com.ironsoftware:ironpdf-engine-linux-x64 針對原生 Chromium 引擎。兩者必須使用相同的版本號。

Dockerfile 必須為 IronPDF 安裝哪些 Linux 套件?

Dockerfile 必須安裝 libgdiplus, libxkbcommon-x11-0, libc6, libc6-dev, libgtk2.0-0, libnss3, libatk-bridge2.0-0, libx11-xcb1, libxcb-dri3-0, libdrm-common, libgbm1, libasound2, libxrender1, libfontconfig1, libxshmfence1, xvfb,並 libva-dev.

RenderPdf 函式的作用是什麼?

RenderPdf 函式是一個由 HTTP 觸發的 Azure Function,它會讀取 url 查詢參數,將其傳遞至 PdfDocument.renderUrlAsPdf,並透過 Content-Disposition: attachment 標頭,以便呼叫者能收到可下載的 PDF 檔案。

IronPDF 應選用哪種 Azure Functions 託管方案?

建議選用 Premium 方案。IronPDF 的 Chromium 引擎需要大量記憶體——通常會超過 Consumption 方案 1.5 GB 的上限。Premium 方案提供至少 3.5 GB 記憶體,並支援預熱實例以消除冷啟動延遲。

為何對新部署函式的首次請求會很慢?

冷啟動後的首次請求可能需要 20 至 60 秒,因為 Azure 必須拉取容器映像檔,且 IronPDF 必須初始化其 Chromium 引擎。在同一容器生命週期內的後續請求回應速度會快得多。Premium 方案的預熱實例功能可消除此延遲。

如何更新現有的 Azure Function App 以使用新的 Docker 映像檔?

重新建置並推送帶有更新標籤的新映像檔,然後再次執行 az functionapp create 並使用新的 --deployment-container-image-name 值再次執行,或在 Azure 入口網站的 Function App「部署中心」中更新容器設定。

IronPDF 能否在 Azure Function 中渲染 HTML 字串,而不僅限於 URL?

是的。請將 PdfDocument.renderUrlAsPdf(url) 替換為 PdfDocument.renderHtmlAsPdf(htmlString) 即可直接渲染 HTML 字串。函式結構與回應處理方式保持不變。

若請求中缺少 URL 查詢參數,會發生什麼情況?

此函式用於檢查 url 參數是否為 null ,並在嘗試任何 PDF 渲染之前,傳回附帶說明訊息的 HTTP 400 Bad Request 回應。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

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