如何在 Azure 函數上執行和部署 IronPDF .NET
是的。 IronPDF 可用於在 Azure 上產生、操作和讀取 PDF 文件。 IronPDF 已在多個 Azure 平台上進行了全面測試,包括 MVC 網站、Azure Functions 等。
如何在 Azure 函數中將 HTML 轉換為 PDF
- 在Azure Function中安裝C#程式庫以將HTML轉換為PDF
- 選擇 Azure Basic B1 託管層或更高
- 在發佈本機套件時,取消勾選
Run from package file選項 - 遵循推薦的配置說明
- 使用代碼範例來創建使用 Azure 的 PDF 生成器
操作指南
安裝IronPDF Package
Azure 函數應用程式有三種不同的環境: Linux 、 Windows和容器。 本文介紹如何在所有三個環境中設置IronPDF。 其中,建議使用 Azure 函數應用程式容器,因為它提供了一個隔離的環境。 首先,讓我們選擇適合的套件來安裝。
Azure 函數應用程式容器
Azure Function App Container涉及的麻煩最少,因此是部署IronPDF的推薦方式。
Install-Package IronPdf.Linux
配置 Docker 文件
根據你使用的Linux發行版配置Docker檔。 請參考本文以獲取詳細說明。
Azure 函數應用程式(Windows)
要使用標準IronPDF套件,請確保Run from package file選項未勾選。 啟用此選項會將專案部署為ZIP文件,這會干擾IronPDF的文件配置。 如果您傾向啟用從套件檔案運行選項,請改為安裝IronPdf.Slim套件,並將CustomDeploymentDirectory設置為應用程式根目錄外可寫的路徑,如D:\home\data\ironsoftware\。
- IronPDF套件

2026.8.1之前,不管您安裝哪個套件,啟用從套件檔案運行都會導致啟動崩潰。 詳情請參閱從封裝執行時的啟動崩潰。Azure 函數應用程式(Linux)
對於 Azure 函數應用程式(Linux),預設情況下專案會部署為 ZIP 文件,並且無法停用此行為。 這類似於在 Azure 函數應用程式(Windows)上啟用「從套件檔案執行」選項。
Install-Package IronPdf.Slim
選擇正確的 Azure 選項
選擇合適的託管級別
Azure Basic B1是滿足我們最終使用者渲染需求的最低託管等級。 如果您正在建置高吞吐量系統,則可能需要對其進行升級。

.NET 6 配置
微軟最近從 .NET 6 及更高版本中移除了映像處理庫,導致許多舊版 API 無法正常運作。因此,您需要配置專案以允許呼叫這些舊版 API。
-
在Linux上,設置
Installation.LinuxAndDockerDependenciesAutoConfig=true;以確保libgdiplus已安裝在機器上 -
將以下內容加入 .NET 6 專案的 .csproj 檔案:
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>XML -
在您的專案中創建一個名為
runtimeconfig.template.json的檔案,並填入以下內容:{ "configProperties": { "System.Drawing.EnableUnixSupport": true } }JSON -
最後,在程式開頭加入以下程式碼行:
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)> req As HttpRequest,
log As ILogger, 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<FunctionName("PrintPdf")>
Public Shared Async Function Run(
<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route:=Nothing)> req As HttpRequest,
log As ILogger, 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:此設置是為寫入權限有限的系統而必須的。 在啟用了從套件檔案運行的Azure App Service上,應用程式根目錄是唯讀的,所以將此指向可寫的路徑,例如Windows上的
D:\home\data\ironsoftware\或Linux上的/home/data/ironsoftware/。
已知問題
共享主機方案不支援 SVG 字體渲染
我們發現的一個限制是Azure託管平台在其較便宜的共享網頁應用程式層不支持伺服器加載SVG字體,例如Google字體。 這是因為出於安全原因,這些共享主機平台不允許存取 Windows GDI+ 圖形物件。
我們建議使用Windows或Linux Docker容器,或者可能是在Azure上的VPS,以解決需要最佳字體渲染的此問題。
Azure 免費方案託管速度慢
Azure 免費層和共用層以及按需付費方案不適合 PDF 渲染。 我們推薦 Azure B1 主機/進階套餐,這也是我們自己使用的套餐。 HTML to PDF的過程對於任何電腦都是重大的「工作」——類似於在您自己的機器上打開和渲染網頁。使用了真正的瀏覽器引擎,因此我們需要相應地進行配置,並預期與具有相似性能的桌面機器相似的渲染時間。
建立工程支援請求工單
若要創建請求單,請參考'如何為IronPDF提出工程支援請求'指導
常見問題
IronPDF可以在Azure上部署嗎?
是的,IronPDF可以部署在Azure上,並且已在多種Azure平台上進行了徹底測試,包括MVC網站和Azure Functions。
推薦在Azure上使用哪個主機層來運行IronPDF?
Azure的Basic B1是運行IronPDF推薦的最低主機層,但對於高吞吐量系統可能需要升級。
為什麼我應該使用Azure Function App Container來部署IronPDF?
建議使用Azure Function App Container來部署IronPDF,因為它提供了一個隔離的環境,並且麻煩最少,特別是在使用IronPdf.Linux套件時。
在Azure上使用IronPDF轉換HTML為PDF的最佳方式是什麼?
要在Azure上使用IronPDF轉換HTML為PDF,您應該安裝C#程式庫,選擇適當的主機方案如Basic B1或更高,並遵循推薦的配置說明。
在Azure上運行.NET 6的IronPDF需要什麼配置?
對於.NET 6,將Install.LinuxAndDockerDependenciesAutoConfig設置為true,在.csproj文件中包含必要的屬性,並創建一個runtimeconfig.template.json來啟用Unix支持。
如何確保在Azure Function App(Windows)中正確部署IronPDF?
確保未選中'Run from package file'選項或使用IronPdf.Slim以避免在部署期間的文件配置問題。
在Azure共享主機方案上使用IronPDF的常見問題是什麼?
Azure的共享主機方案不支持加載SVG字體,並且可能因限制訪問Windows GDI+圖形對象而限制IronPDF的圖形功能。
Azure免費層適合IronPDF嗎?
不,Azure免費層和共享主機方案通常對IronPDF任務來說過慢。我們建議使用Azure的B1主機或Premium方案以獲得更好的性能。
如何解決Azure Function專案之間的部署差異?
如果出現部署差異,請確保CustomDeploymentDirectory設置為'/tmp',並檢查您的特定設置是否需要任何額外配置。
在Azure上,IronPDF有哪些日誌記錄選項?
IronPDF允許通過Azure的內建記錄器進行記錄,通過啟用自定義日誌模式並將CustomLogger設置為所需日誌。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。