如何在 Azure 功能上运行和部署 IronPDF for .NET

是的。 IronPDF可用于在Azure上生成、操作和读取PDF文档。 IronPDF已在多个Azure平台上进行了彻底测试,包括MVC网站、Azure Functions等等。
如何在 Azure 函数中制作 PDF 生成器
- 安装 C# 库在 Azure 中生成 PDF
- 选择 "Azure 基本 B1 "或以上托管层级
- 取消选中
从软件包文件运行
选项时 - 按照建议的配置说明进行配置
- 使用代码示例创建使用 Azure 的 PDF 生成器
教程
安装 IronPdf 软件包
Azure 功能应用程序有三种不同的环境: Linux、Windows和容器。 本文将介绍如何在这三种环境中设置 IronPdf。 在这些工具中,推荐使用 Azure Function App Container,因为它提供了一个隔离的环境。 首先,让我们选择合适的软件包进行安装。
Azure 功能应用程序容器
Azure Function App Container 带来的麻烦最少,因此是部署 IronPdf 的推荐方式。
- IronPdf.Linux包装
安装-打包 IronPdf.Linux
配置 Docker 文件
根据您使用的 Linux 发行版配置 Docker 文件。 请参阅本条有关详细说明,请参阅。
Azure 功能应用程序(Windows)
使用标准IronPdf在翻译软件包时,请确保从软件包文件运行选项未被选中。 启用此选项会将项目部署为 ZIP 文件,这会干扰 IronPdf 的文件配置。 如果您希望启用从软件包文件运行选项,请安装IronPdf.Slim而不是打包。
- IronPdf包装
Install-Package IronPdf

Azure 功能应用程序(Linux)
针对 Azure 功能应用程序(Linux)此外,项目默认以 ZIP 文件格式部署,且无法禁用。 这类似于在 Azure 功能应用程序上启用从包文件运行选项(Windows).
- IronPdf.Slim包装
安装-打包 IronPdf.Slim
选择正确的 Azure 选项
选择正确的托管层级
Azure Basic B1 是满足我们最终用户渲染需求的最低托管级别。 如果您正在创建一个高吞吐量的系统,可能需要对其进行升级。
在继续之前

针对 .NET 6 的配置
Microsoft最近从.NET 6+中删除了成像库,破坏了许多遗留API。因此,有必要配置您的项目以便仍然允许这些遗留API调用。
-
在Linux上,设置
Installation.LinuxAndDockerDependenciesAutoConfig=true;
以确保机器上安装了libgdiplus
。 -
将以下内容添加到您的 .NET 6 项目的.csproj 文件中:
真 ` - 在您的项目中创建一个名为
runtimeconfig.template.json
的文件,并用以下内容填充:
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
- 最后,在程序开头添加以下一行:System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);`
Azure函数代码示例
此示例会自动将日志条目输出到内置的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 log
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
IronPdf.Logging.Logger.CustomLogger = log;
// Configure IronPdf
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
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", e);
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 log
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
IronPdf.Logging.Logger.CustomLogger = log;
// Configure IronPdf
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
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", e);
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 log
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom
IronPdf.Logging.Logger.CustomLogger = log
' Configure IronPdf
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
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", e)
Return New OkObjectResult($"Error while rendering pdf: {e}")
End Try
End Function
使用 Visual Studio 中的 Azure 函数模板创建项目可能会导致代码略有不同。 由于存在这些差异,即使安装了相同的软件包,一个项目也可能可以运行,而另一个项目则不行。 如果出现这种情况,请将CustomDeploymentDirectory属性设置为"/tmp "。
了解每个安装配置
- LinuxAndDockerDependenciesAutoConfig:此设置会检查并尝试下载 Chrome 引擎的所有必要依赖项。使用 Linux 等非图形用户界面系统时需要使用该设置。 在容器系统中,依赖项通常列在 Dockerfile 中;因此,您可以将其设置为 false。
- 自动下载本地文件:该选项可在运行时下载 Chrome 浏览器的本地二进制文件。使用 IronPdf.Slim 软件包时需要此选项。
- 自定义部署目录:写入访问受限的系统需要此设置。
已知问题
SVG字体渲染在共享主机计划中不可用。
我们发现的一个限制是Azure 托管平台不支持在其较便宜的共享Web应用层中加载SVG字体(例如Google Fonts)的服务器。 这是因为出于安全原因,这些共享托管平台不允许访问Windows GDI+图形对象。
我们建议使用一个Windows 或 Linux Docker 容器或 Azure 上的 VPS,以解决需要最佳字体渲染的问题。
Azure 免费层托管速度慢
Azure 免费和共享层,以及消费计划,不适合用于 PDF 渲染。 我们推荐使用Azure B1托管/高级计划,这也是我们自己使用的方案。 将HTML转为PDF
的过程对任何计算机来说都是一项重要的'工作' - 类似于在自己的机器上打开和渲染网页。使用了真正的浏览器引擎,因此我们需要相应地做好准备,并期望与具有相似性能的桌面机器有相似的渲染时间。
创建工程支持请求工单
为了创建请求票务,请参考 '如何提出IronPDF的工程支持请求'指南