如何在Azure Function上运行和部署IronPDF .NET
是的。 IronPDF 可以用于在 Azure 上生成、操作和读取 PDF 文档。 IronPDF 已在多个 Azure 平台上经过充分测试,包括 MVC 网站,Azure Functions 等。
如何在 Azure 函数中将 HTML 转换为 PDF
- 在Azure Function中安装用于将HTML转换为PDF的C#库
- 选择 Azure Basic B1 或更高的托管层
- 发布本机包时取消选择
从包文件运行选项 - 按照推荐的配置说明进行操作
- 使用代码示例在 Azure 上创建 PDF 生成器
如何使用教程
安装IronPDF包
Azure功能应用程序有三个不同的环境:Linux、Windows和容器。 本文解释了如何在所有三个环境中设置IronPDF。 其中,推荐Azure功能应用程序容器,因为它提供了一个隔离的环境。 首先,让我们选择要安装的合适包。
Azure功能应用程序容器
Azure Function App Container涉及的麻烦最少,因此是推荐的IronPDF部署方式。
Install-Package IronPdf.Linux
配置Docker文件
根据您正在使用的Linux发行版配置Docker文件。 请参阅本文以获取详细说明。
Azure功能应用程序(Windows)
要使用标准IronPDF包,请确保未勾选从包文件运行选项。 启用此选项会将项目作为ZIP文件部署,这会干扰IronPDF的文件配置。 如果您更喜欢启用 从包文件运行 选项,请安装 IronPdf.Slim 包,并将 CustomDeploymentDirectory 设置为应用程序根目录之外的可写路径,例如 D:\home\data\ironsoftware\。
- IronPDF 包

2026.8.1之前的IronPDF版本中,启用从包文件运行会导致启动崩溃,无论您安装哪个包。 查看启动崩溃与从包运行以获取详细信息。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 Function模板创建项目可能导致代码略有不同。 由于这些差异,即使安装了相同的软件包,一个项目可能会工作,而另一个则不会。 如果出现这种情况,请将CustomDeploymentDirectory属性设置为"/tmp"。
了解每个安装配置
- LinuxAndDockerDependenciesAutoConfig:此设置检查并尝试下载Chrome引擎所需的所有依赖项。使用非GUI系统(如Linux)时需要。 在容器系统中,依赖项通常列在Dockerfile中;因此,您可以将此设置为false。
- AutomaticallyDownloadNativeBinaries:此选项会在运行时下载本地Chrome二进制文件。使用IronPdf.Slim包时需要。
- CustomDeploymentDirectory:此设置适用于写入权限有限的系统。 在启用 从包文件运行 的 Azure App Service 上,应用程序根目录是只读的,因此将其指向可写路径,例如 Windows 上的
D:\home\data\ironsoftware\或 Linux 上的/home/data/ironsoftware/。
已知问题
共享托管计划上不支持SVG字体渲染
我们发现一个限制是Azure托管平台不支持在其较便宜的共享web应用套餐中加载SVG字体(如Google Fonts)的服务器。 这是因为这些共享托管平台出于安全原因不允许访问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推荐的托管层是哪个?
运行IronPDF推荐的最低Azure托管层是Azure Basic B1,但对于高吞吐量系统可能需要升级。
为什么我应该选择Azure功能应用容器来使用IronPDF?
推荐使用Azure功能应用容器来部署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功能应用程序(Windows)中正确部署IronPDF?
确保取消选中'从包文件运行'选项或使用IronPdf.Slim以避免部署期间的文件配置问题。
在Azure共享托管计划中,IronPDF常见的问题是什么?
Azure上的共享托管计划不支持加载SVG字体,并可能由于限制访问Windows GDI+图形对象而对IronPDF的图形能力有限制。
Azure免费层适合IronPDF吗?
不,Azure免费层和共享托管计划通常对于IronPDF任务来说太慢。我们建议使用Azure B1托管或高级计划以获得更好的性能。
如何解决Azure功能项目之间的部署差异?
如果出现部署差异,请确保将CustomDeploymentDirectory设置为'/tmp',并检查您的特定设置是否需要任何其他配置。
在Azure上,IronPDF有哪些日志记录选项?
IronPDF通过启用自定义日志模式并将CustomLogger设置为所需日志,允许通过Azure内置的记录器进行日志记录。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。