如何在 Azure 函數上執行和部署IronPDF .NET
是的。 IronPDF可用於在 Azure 上產生、操作和讀取 PDF 文件。 IronPDF已在多個 Azure 平台上進行了全面測試,包括 MVC 網站、Azure Functions 等。
如何在 Azure 函數中將 HTML 轉換為 PDF
- 在 Azure 函數中安裝 C# 程式庫以將 HTML 轉換為 PDF
- 選擇 Azure 基本 B1 託管層或更高版本
- 發佈時取消勾選`Run from package file`選項
- 請按照建議的配置說明進行操作
- 使用程式碼範例,透過 Azure 建立 PDF 產生器。
操作指南
安裝IronPDF包
Azure 函數應用程式有三種不同的環境: Linux 、 Windows和容器。 本文介紹如何在所有三種環境中設定IronPDF 。 其中,建議使用 Azure 函數應用程式容器,因為它提供了一個隔離的環境。 首先,讓我們選擇要安裝的合適軟體包。
Azure 函數應用程式容器
Azure 函數應用程式容器使用起來非常方便,因此是部署IronPDF 的建議方式。
Install-PackageIronPdf.Linux 軟體包
配置 Docker 文件
根據你使用的Linux發行版配置Docker檔。 請參閱本文以取得詳細說明。
Azure 函數應用程式(Windows)
若要使用標準的IronPDF包,請確保"從包檔案執行"選項未選取。 啟用此選項會將項目部署為 ZIP 文件,這會幹擾 IronPdf 的文件配置。 如果您希望啟用"從套件檔案執行"選項,請安裝IronPdf.Slim套件。
Install-Package IronPdf
Azure 函數應用程式(Linux)
對於 Azure 函數應用程式(Linux),預設情況下專案會部署為 ZIP 文件,並且無法停用此行為。 這類似於在 Azure 函數應用程式(Windows)上啟用"從套件檔案執行"選項。
Install-PackageIronPDF包
選擇正確的 Azure 選項
選擇合適的託管級別
Azure Basic B1是滿足我們最終使用者渲染需求的最低託管等級。 如果您正在建置高吞吐量系統,則可能需要對其進行升級。
.NET 6 配置
微軟最近從.NET 6 及更高版本中移除了映像處理庫,導致許多舊版 API 無法正常運作。因此,您需要配置專案以允許呼叫這些舊版 API。
- 在 Linux 系統上,設定
Installation.LinuxAndDockerDependenciesAutoConfig=true;以確保libgdiplus已安裝在機器上。 - 將以下內容加入.NET 6 專案的 .csproj 檔案:
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
- 在你的專案中建立一個名為
runtimeconfig.template.json的文件,並填入以下內容:
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
- 最後,在程式開頭加入以下程式碼行:
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", True)
Azure 函數程式碼範例
此範例將 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
在 Visual Studio 中使用 Azure 函數範本建立專案可能會產生略有不同的程式碼。 由於這些差異,即使安裝了相同的軟體包,一個專案可能可以運行,而另一個專案可能無法運行。 如果發生這種情況,請將CustomDeploymentDirectory屬性設為"/tmp" 。
了解每種安裝配置
- LinuxAndDockerDependenciesAutoConfig :此設定會檢查並嘗試下載 Chrome 引擎所需的所有相依性。在使用非圖形使用者介面系統(例如 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 主機/進階套餐,這也是我們自己使用的套餐。 對於任何計算機來說,處理 HTML to 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 提交工程支持請求》的指南以獲取詳細幫助。

