如何在 Azure Function 上运行和部署 IronPDF .NET
是的。 IronPDF 可在 Azure 上用于生成、处理和读取 PDF 文档。 IronPDF 已在包括 MVC 网站、Azure Functions 在内的多个 Azure 平台上经过全面测试。
如何在 Azure Function 中将 HTML 转换为 PDF
- 在 Azure Function 中安装 C# 库以将 HTML 转换为 PDF
- 请选择 Azure Basic B1 及以上托管层级
- 发布时取消勾选
Run from package file选项 - 请遵循推荐的配置说明
- 使用代码示例通过 Azure 创建 PDF 生成器
操作指南
安装 IronPDF 软件包
Azure Function Apps 拥有三个不同的运行环境:Linux、Windows 和容器。 本文介绍了如何在这三种环境中配置 IronPDF。 其中,建议使用 Azure Function App Container,因为它提供了一个隔离的环境。 首先,让我们选择合适的软件包进行安装。
Azure Function App Container
Azure Function App Container 部署过程简便,因此是部署 IronPDF 的推荐方案。
- IronPdf.Linux 软件包
Install-Package IronPdf.Linux
配置 Docker 文件
请根据您使用的 Linux 发行版配置 Dockerfile。 请参阅本文获取详细说明。
Azure Function App (Windows)
若要使用标准版 IronPDF 软件包,请确保"从软件包文件运行"选项未被勾选。 启用此选项会将项目部署为 ZIP 文件,这会干扰 IronPDF 的文件配置。 如果您希望启用"从包文件运行"选项,请改安装 IronPdf.Slim 包。
- IronPDF 软件包
Install-Package IronPdf
Azure Function App (Linux)
对于 Azure Function App (Linux),该项目默认以 ZIP 文件形式部署,且此行为无法禁用。 这类似于在 Azure Function App(Windows)上启用"从包文件运行"选项。
- IronPdf.Slim 软件包
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>
<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 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
在 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 托管/高级套餐,这也是我们自己正在使用的方案。 HTML to PDF 的处理过程对任何计算机而言都是一项"重负"——类似于在本地机器上打开并渲染网页。由于使用了真正的浏览器引擎,因此我们需要相应地配置资源,并预期其渲染时间与同等配置的桌面电脑相当。
创建工程支持请求工单
如需创建支持工单,请参阅《如何提交 IronPDF 工程支持请求》指南
常见问题解答
如何在 Azure 上托管一个 PDF 生成库?
您可以通过设置 Azure 功能或 MVC 网站来托管类似 IronPDF 的 PDF 生成库。确保您有必要的 NuGet 包安装,并根据库的要求配置您的环境。
哪些 Azure 环境与 PDF 库兼容?
IronPDF 兼容多个 Azure 环境,包括 MVC 网站和 Azure Functions。它被设计为在 Azure 提供的不同平台上无缝运行。
PDF 库在 Azure 上的托管要求是什么?
为确保在 Azure 上使用 IronPDF 时的最佳性能,建议至少使用 Azure Basic B1 托管层。这可确保有足够的资源来有效呈现 PDF。
如何在 Linux 上为 Azure 功能应用设置 PDF 库?
要在 Linux 上为 Azure 功能应用设置 IronPDF,请安装 IronPdf.Slim 包。将您的项目部署为 ZIP 文件,并确保所有配置正确设置以适应Linux 环境。
.NET 6 在 Azure 上使用 PDF 库时需要哪些配置?
在 Azure 上使用 .NET 6 的 IronPDF 时,请更新项目设置以允许旧版 API 调用。将 Installation.LinuxAndDockerDependenciesAutoConfig=true 设置,并在项目文件中包含必要的配置。
为什么推荐在 Azure 上进行 PDF 呈现时使用 Docker 容器?
建议在 Azure 上进行 PDF 呈现时使用 Docker 容器,因为它提供了更受控的环境,支持更好的字体呈现,避免了共享托管计划的限制。
什么可能导致 Azure 免费层的 PDF 渲染速度缓慢?
Azure 免费层的 PDF 渲染速度缓慢是由于计算资源有限。该过程需要大量计算能力,类似于渲染一个网页,使 B1 或 Premium 这样更高等级的计划更为合适。
如何排除在 Azure 功能项目中 PDF 库不起作用的问题?
如果您的 Azure 功能项目中的 IronPDF 无法运行,请检查 CustomDeploymentDirectory 属性是否设置为 '/tmp',并确保所有必要的包和配置已正确安装和设置。
在 Azure 上部署 PDF 库有哪些支持选项?
要获得在 Azure 上部署 IronPDF 的支持,请访问 IronPDF 网站上的“如何为 IronPDF 提交工程支持请求”指南以获取详细帮助。

