如何在 Azure Function 上執行與部署 IronPDF for .NET

This article was translated from English: Does it need improvement?
Translated
View the article in English
Azure 1 related to 如何在 Azure Function 上執行與部署 IronPDF for .NET

是的。 IronPDF 可用於在 Azure 上建立、處理及讀取 PDF 文件。 IronPDF 已在多個 Azure 平台上經過徹底測試,包括 MVC 網站、Azure Functions 以及更多平台。


操作教學

安裝 IronPDF 套件

Azure Function Apps 具備三種不同的執行環境:LinuxWindowsContainer。 本文說明如何在上述三種環境中設定 IronPDF。 其中,建議使用 Azure Function App Container,因為它提供了一個隔離的環境。 首先,讓我們選擇合適的套件進行安裝。

Azure Function App Container

Azure Function App Container 的部署過程極為簡便,因此是部署 IronPDF 的首選方式。

Install-Package IronPdf.Linux

設定 Docker 檔案

請根據您使用的 Linux 發行版來設定 Docker 檔案。 請參閱此文章以獲取詳細說明。

Azure Function App (Windows)

若要使用標準版 IronPDF 套件,請確保"從套件檔案執行"選項未被勾選。 啟用此選項會將專案部署為 ZIP 檔案,這會干擾 IronPDF 的檔案設定。 若您希望啟用"從套件檔案執行"選項,請改為安裝 IronPdf.Slim 套件。

Install-Package IronPdf
Azure Package File related to Azure Function App (Windows)

Azure Function App (Linux)

對於 Azure Function App (Linux),該專案預設會以 ZIP 檔案形式部署,且此行為無法停用。 這類似於在 Azure Function App (Windows) 上啟用"從套件檔案執行"選項。

Install-Package IronPdf.Slim

選擇正確的 Azure 選項

選擇合適的主機方案

Azure Basic B1 是滿足我們終端使用者渲染需求所需的最低託管層級。 若您正在建置高吞吐量系統,可能需要進行升級。

警告注意:若未選擇"App 服務方案"作為方案類型,可能會導致 IronPDF 無法渲染 PDF 文件。

選擇正確的 Azure 服務層級

.NET 6 的設定

微軟近期已從 .NET 6+ 中移除影像處理函式庫,導致許多舊版 API 無法運作。因此,您必須設定專案以繼續允許呼叫這些舊版 API。

  1. 在 Linux 系統上,請設定 Installation.LinuxAndDockerDependenciesAutoConfig=true; 以確保 libgdiplus 已安裝於該機器上
  2. 請在您的 .NET 6 專案的 .csproj 檔案中加入以下內容:
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
XML
  1. 在您的專案中建立一個名為 runtimeconfig.template.json 的檔案,並填入以下內容:
{
  "configProperties": {
    "System.Drawing.EnableUnixSupport": true
  }
}
  1. 最後,請在程式開頭加入以下這行:
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", True)
$vbLabelText   $csharpLabel

Azure Function 程式碼範例

此範例將 HTML 轉換為 PDF,並自動將日誌條目輸出至內建的 Azure 日誌記錄器(參見 ILogger log)。

[FunctionName("PrintPdf")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log, ExecutionContext context)
{
    log.LogInformation("Entered PrintPdf API function...");

    // Apply license key
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";

    // Enable logging
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;

    // Configure IronPdf settings
    IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
    IronPdf.Installation.AutomaticallyDownloadNativeBinaries = true;
    IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    IronPdf.Installation.CustomDeploymentDirectory = "/tmp";

    try
    {
        log.LogInformation("About to render PDF...");
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        // Render PDF from a URL
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");
        log.LogInformation("Finished rendering PDF...");
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering PDF");
        return new OkObjectResult($"Error while rendering PDF: {e}");
    }
}
[FunctionName("PrintPdf")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log, ExecutionContext context)
{
    log.LogInformation("Entered PrintPdf API function...");

    // Apply license key
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";

    // Enable logging
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;

    // Configure IronPdf settings
    IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
    IronPdf.Installation.AutomaticallyDownloadNativeBinaries = true;
    IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    IronPdf.Installation.CustomDeploymentDirectory = "/tmp";

    try
    {
        log.LogInformation("About to render PDF...");
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        // Render PDF from a URL
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");
        log.LogInformation("Finished rendering PDF...");
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering PDF");
        return new OkObjectResult($"Error while rendering PDF: {e}");
    }
}
<FunctionName("PrintPdf")>
Public Shared Async Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger, ByVal context As ExecutionContext) As Task(Of IActionResult)
	log.LogInformation("Entered PrintPdf API function...")

	' Apply license key
	IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"

	' Enable logging
	IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom
	IronPdf.Logging.Logger.CustomLogger = log

	' Configure IronPdf settings
	IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
	IronPdf.Installation.AutomaticallyDownloadNativeBinaries = True
	IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
	IronPdf.Installation.CustomDeploymentDirectory = "/tmp"

	Try
		log.LogInformation("About to render PDF...")
		Dim renderer As New ChromePdfRenderer()
		' Render PDF from a URL
		Dim pdf = renderer.RenderUrlAsPdf("https://www.google.com/")
		log.LogInformation("Finished rendering PDF...")
		Return New FileContentResult(pdf.BinaryData, "application/pdf") With {.FileDownloadName = "google.pdf"}
	Catch e As Exception
		log.LogError(e, "Error while rendering PDF")
		Return New OkObjectResult($"Error while rendering PDF: {e}")
	End Try
End Function
$vbLabelText   $csharpLabel

若在 Visual Studio 中使用 Azure Function 範本建立專案,生成的程式碼可能會略有不同。 由於這些差異,即使安裝了相同的套件,一個專案可能運作正常,而另一個卻無法運作。 若發生此情況,請將 CustomDeploymentDirectory 屬性設定為 "/tmp"

了解各項安裝設定

  • LinuxAndDockerDependenciesAutoConfig:此設定會檢查並嘗試下載 Chrome 引擎所需的所有依賴項。當使用非 GUI 系統(例如 Linux)時,此設定為必要條件。 在容器系統中,依賴項通常會列於 Dockerfile 中;因此,您可以將此選項設為 false。
  • AutomaticallyDownloadNativeBinaries:此選項會在執行時自動下載原生 Chrome 二進位檔。使用 IronPdf.Slim 套件時,此選項為必選項目。
  • CustomDeploymentDirectory:此設定適用於寫入權限受限的系統。

已知問題

共用主機方案不支援 SVG 字型渲染

我們發現的一項限制是,Azure 託管平台在其較低價位的共享 Web 應用程式層級中,不支援伺服器載入 SVG 字型(例如 Google Fonts)。 這是因為基於安全考量,這些共享主機平台不允許存取 Windows GDI+ 圖形物件。

我們建議使用 Windows 或 Linux Docker 容器,或是在 Azure 上架設 VPS,以解決此問題,確保能呈現最佳的字型渲染效果。

Azure 免費方案的託管速度較慢

Azure 的免費和共享層級,以及用量方案,均不適用於 PDF 渲染。 我們建議選用 Azure B1 託管/Premium 方案,這也是我們自己使用的方案。 HTML to PDF 的處理過程對任何電腦而言都是一項"重負"——類似於在您自己的電腦上開啟並渲染網頁。由於採用了真正的瀏覽器引擎,因此我們需要進行相應的資源配置,並預期其渲染時間將與同等效能的桌面電腦相仿。

建立工程支援請求單

如需建立支援請求單,請參閱《如何針對 IronPDF 提交工程支援請求》指南

常見問題

如何在 Azure 上託管 PDF 生成函式庫?

您可以透過設定 Azure Function 或 MVC 網站,在 Azure 上部署 IronPDF 等 PDF 產生函式庫。請確保已從 NuGet 安裝必要的套件,並依照函式庫的要求配置您的環境。

哪些 Azure 環境與 PDF 函式庫相容?

IronPDF 相容於多種 Azure 環境,包括 MVC 網站和 Azure Functions。其設計旨在於 Azure 提供的不同平台間無縫運作。

在 Azure 上部署 PDF 函式庫有哪些託管需求?

若要在 Azure 上使用 IronPDF 並獲得最佳效能,建議至少選用 Azure Basic B1 託管層級。此舉可確保有充足資源以高效渲染 PDF 檔案。

如何在 Linux 上為 Azure Function App 設定 PDF 函式庫?

要在 Linux 上的 Azure Function App 中設定 IronPDF,請安裝 IronPdf.Slim 套件。將您的專案以 ZIP 檔案形式部署,並確保所有設定均已正確配置以適用於 Linux 環境。

在 Azure 上使用 PDF 函式庫時,.NET 6 需要哪些設定?

若要在 Azure 上將 IronPDF for .NET 6 搭配使用,請更新專案設定以允許舊版 API 呼叫。請將 Installation.LinuxAndDockerDependenciesAutoConfig 設為 true,並在專案檔案中加入必要的設定。

為何建議在 Azure 上使用 Docker 容器進行 PDF 渲染?

建議在 Azure 上使用 Docker 容器進行 PDF 渲染,因為它能提供更受控的環境、支援更佳的字型渲染,並可避免共享主機方案的限制。

什麼原因會導致 Azure 免費層級的 PDF 渲染速度變慢?

由於運算資源有限,在 Azure 免費方案中,PDF 渲染速度較慢。此過程需要相當大的運算能力,類似於渲染網頁,因此 B1 或 Premium 等更高階的方案更為適合。

如何排除使用 PDF 函式庫時無法運作的 Azure Function 專案問題?

若您的 Azure Function 專案中 IronPDF 無法正常運作,請檢查 CustomDeploymentDirectory 屬性是否設定為 '/tmp',並確保所有必要的套件與設定皆已正確安裝及設定。

在 Azure 上部署 PDF 函式庫有哪些支援選項?

若需協助在 Azure 上部署 IronPDF,請參閱 IronPDF 網站上的《如何提交 IronPDF 工程支援請求》指南,以獲取詳細協助。

Curtis Chau
技術撰稿人

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

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

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。