How to Run & Deploy IronPDF .NET on Azure Function

This article was translated from English: Does it need improvement?
Translated
View the article in English
class="container-fluid">
class="row">
class="col-md-2"> Azure 1 related to How to Run & Deploy IronPDF .NET on Azure Function

是。 IronPDF 可以在 Azure 上生成、處理和閱讀 PDF 文檔。 IronPDF 在多個 Azure 平台上經過了徹底測試,包括 MVC 網站,Azure Functions 等。

class="hsg-featured-snippet">

如何在 Azure Function 中將 HTML 轉換為 PDF

  1. 安裝 C# 庫在 Azure Function 中將 HTML 轉換為 PDF
  2. 選擇 Azure Basic B1 託管層或更高版本
  3. 發布時取消選中 從包文件運行 選項
  4. 遵循推薦的配置說明
  5. 使用代碼示例使用 Azure 創建 PDF 生成器

class="main-content__segment-title">如何指南

安裝 IronPdf 包

Azure Function Apps 有三個不同的環境:LinuxWindows容器。 本文解釋了如何在三種環境中設置 IronPdf。 其中推薦 Azure Function App 容器,因為它提供了隔離環境。 首先,讓我們選擇要安裝的合適包。

Azure Function App 容器

Azure Function App 容器涉及的麻煩最少,是部署 IronPdf 的推薦方式。

data-bs-title="已複製到剪貼板">
Install-Package IronPdf.Linux

配置 Docker 文件

根據您使用的 Linux 發行版配置 Docker 文件。 請參考本文獲取詳細說明。

Azure Function App (Windows)

要使用標準 IronPdf 包,請確保從包文件運行選項未選中。 啟用此選項會將項目部署為 ZIP 文件,這會干擾 IronPdf 的文件配置。 如果您希望啟用 從包文件運行選項,請安裝 IronPdf.Slim 包。

Install-Package IronPdf
class="content-img-align-center">
class="center-image-wrapper"> Azure Package File related to Azure Function App (Windows)

Azure Function App (Linux)

對於 Azure Function App (Linux),默認情況下項目以 ZIP 文件形式部署,這種行為無法禁用。 這類似於在 Azure Function App (Windows) 上啟用 從包文件運行選項。

data-bs-title="已複製到剪貼板">
Install-Package IronPdf.Slim

選擇正確的 Azure 選項

選擇正確的託管層

Azure Basic B1 是我們最終用戶渲染需求所需的最低託管級別。 如果您正在創建高吞吐量系統,則可能需要升級。

警告注意: 未能選擇 App service plan 的計劃類型可能會導致 IronPdf 無法渲染 PDF 文檔。

class="content-img-align-center">
class="center-image-wrapper"> 為 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 託管平台 不支持加載 SVG 字體(如 Google 字體)的服務器,特別是在其更便宜的共享 Web 應用程序計劃。 這是因為這些共享託管平台不允許訪問 Windows 的 GDI+ 圖形對象以出於安全原因。

我們建議使用Windows 或 Linux Docker 容器,或可能是需要最佳字體渲染的一個 Azure VPS 來應對此問題。

Azure 免費層託管速度慢

Azure 的免費和共享層,以及消耗計劃,不適合 PDF 渲染。 我們建議使用 Azure B1 託管/高級計劃,這也是我們使用的方案。 HTML 到 PDF 的處理對任何計算機來說都是重要的“工作”——類似於在自己機器上打開和渲染網頁。使用了真實的瀏覽器引擎,因此我們需要適當地配置並預期到與對應功率的桌面機器相似的渲染時間。

創建工程支持請求票

為了創建支持票請參考 '如何為 IronPDF 提交工程支持請求' 指南

常見問題解答

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

您可以通過設置 Azure Function 或 MVC 網站來在 Azure 上託管像 IronPDF 這樣的 PDF 生成庫。確保從 NuGet 安裝必要的包,並根據庫的要求配置您的環境。

哪些 Azure 環境與 PDF 庫兼容?

IronPDF 與多個 Azure 環境兼容,包括 MVC 網站和 Azure 函數。它設計為在 Azure 提供的不同平台上無縫工作。

在 Azure 上託管 PDF 庫的要求是什麼?

為了在 Azure 上使用 IronPDF 時獲得最佳性能,建議至少使用 Azure Basic B1 託管層。這可確保 PDF 渲染時有足夠的資源。

我如何為 Azure Function App 在 Linux 上設置 PDF 庫?

要為 Linux 上的 Azure Function App 設置 IronPDF,請安裝 IronPdf.Slim 套件。將您的項目部署為 ZIP 文件,並確保所有配置正確設置以適應 Linux 環境。

在 Azure 上使用 PDF 庫時 .NET 6 需要哪些配置?

當在 Azure 上使用 IronPDF 和 .NET 6 時,更新您的項目設置以允許遺留 API 調用。設置 Installation.LinuxAndDockerDependenciesAutoConfig=true 並在項目文件中包含必要的配置。

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

建議在 Azure 上進行 PDF 渲染時使用 Docker 容器是因為它提供了更受控的環境,支持更好的字體渲染,並且避免了共享託管計畫的限制。

什麼可能導致在 Azure Free Tier 上 PDF 渲染速度變慢?

在 Azure Free Tier 上 PDF 渲染速度變慢是由於計算資源有限。此過程需要大量的計算能力,類似於渲染網頁,因此更高層級的計畫如 B1 或 Premium 更為合適。

如何排除使用 PDF 庫的 Azure Function 項目無法運行的故障?

如果您的 Azure Function 項目與 IronPDF 不運行,檢查 CustomDeploymentDirectory 屬性是否設置為 '/tmp',並確保所有必要的包和配置都正確安裝和設置。

部署 PDF 庫在 Azure 上有哪些支持選項?

有關在 Azure 上部署 IronPDF 的支持,請參考 IronPDF 網站上的《如何為 IronPDF 提交工程支持請求》的指南以獲取詳細幫助。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布