如何使用.NET在Azure上生成HTML到PDF

How to Run HTML to PDF with .NET on Azure?

This article was translated from English: Does it need improvement?
Translated
View the article in English

是的。 IronPDF 可以用于在 Azure 上生成、操作和读取 PDF 文档。 IronPDF 已在多个 Azure 平台上经过充分测试,包括 MVC 网站,Azure Functions 等。

如果您在 Docker 容器中运行 Azure Functions,请参阅本 Azure Docker Linux 教程

快速入门:在 Azure 上使用 IronPDF 进行 HTML 到 PDF 转换

使用 IronPDF 在您的 Azure 应用程序中毫不费力地开始转换 HTML 到 PDF。 本快速指南演示了如何使用 IronPDF 高效的 API 方法将 URL 渲染为 PDF 文档。 对于希望在其 Azure 解决方案中集成 PDF 功能的开发人员来说,此示例展示了 IronPDF 的简单性和速度,确保您的 PDF 生成任务无缝且时间高效。 按照示例开始生成 PDF 而不丢失格式,让您的 Azure 项目快速启动并运行。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = new IronPdf.ChromePdfRenderer()
        .RenderHtmlAsPdf("<h1>Hello Azure!</h1>")
        .SaveAs("output‑azure.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 安装 C# 库以在 Azure 中生成 PDF
  2. 选择 Azure Basic B1 托管层或以上
  3. 发布时取消选中 从软件包文件运行 选项
  4. 遵循推荐的配置说明
  5. 使用代码示例在 Azure 上创建 PDF 生成器

class="main-content__segment-title">教程如何做

设置您的项目

安装 IronPDF 以开始

第一步是使用 NuGet 安装 IronPDF:

Install-Package IronPdf

或者,通过使用 Azure 的 IronPDF 直接下载包 链接手动安装 .dll。

选择正确的 Azure 选项

选择正确的托管级别 Azure 层

Azure Basic B1 是我们最终用户渲染需求的最低托管级别。 如果您正在创建一个高吞吐量系统,这可能需要升级。

警告未选择应用服务计划的计划类型可能会导致 IronPdf 无法渲染 PDF 文档。

class="content-img-align-center">
class="center-image-wrapper"> 选择正确的托管级别 Azure 层

“从软件包文件运行”复选框

发布您的 Azure Functions 应用程序时,请确保未选择 从软件包文件运行

class="content-img-align-center">
class="center-image-wrapper"> 取消选中从软件包文件运行选项

为 .NET 6 配置

微软最近从 .NET 6+ 中移除了成像库,破坏了许多遗留的 API。因此,有必要配置您的项目以仍然允许这些遗留的 API 调用。

  1. 在 Linux 上,设置 Installation.LinuxAndDockerDependenciesAutoConfig=true; 以确保在机器上安装了 libgdiplus
  2. 为您的 .NET 6 项目在 .csproj 文件中添加以下内容:
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    XML
  3. 在您的项目中创建一个名为 runtimeconfig.template.json 的文件,并填充以下内容:
{
  "configProperties": {
    "System.Drawing.EnableUnixSupport": true
  }
}
  1. 最后,添加以下行到程序的开头以启用 Unix 支持的 System.Drawing:
    System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
    System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
    System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", True)
    $vbLabelText   $csharpLabel

在 Azure 上使用 Docker

获得控制、SVG 字体访问以及控制性能的一种方法是在 Docker 容器中使用 IronPDF 应用和函数。

我们有一个全面的IronPDF Azure Docker 教程,适用于 Linux 和 Windows 实例,建议阅读。

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";

    // Configure logging
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;
    IronPdf.Logging.Logger.EnableDebugging = false;

    // Configure IronPdf settings
    Installation.LinuxAndDockerDependenciesAutoConfig = false;
    Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

    try
    {
        log.LogInformation("About to render pdf...");

        // Create a renderer and render the URL as PDF
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");

        log.LogInformation("Finished rendering pdf...");

        // Return the rendered PDF as a file download
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering pdf");
    }

    return new OkObjectResult("OK");
}
[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";

    // Configure logging
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;
    IronPdf.Logging.Logger.EnableDebugging = false;

    // Configure IronPdf settings
    Installation.LinuxAndDockerDependenciesAutoConfig = false;
    Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

    try
    {
        log.LogInformation("About to render pdf...");

        // Create a renderer and render the URL as PDF
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");

        log.LogInformation("Finished rendering pdf...");

        // Return the rendered PDF as a file download
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering pdf");
    }

    return new OkObjectResult("OK");
}
<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"

	' Configure logging
	IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom
	IronPdf.Logging.Logger.CustomLogger = log
	IronPdf.Logging.Logger.EnableDebugging = False

	' Configure IronPdf settings
	Installation.LinuxAndDockerDependenciesAutoConfig = False
	Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled

	Try
		log.LogInformation("About to render pdf...")

		' Create a renderer and render the URL as PDF
		Dim renderer As New ChromePdfRenderer()
		Dim pdf = renderer.RenderUrlAsPdf("https://www.google.com/")

		log.LogInformation("Finished rendering pdf...")

		' Return the rendered PDF as a file download
		Return New FileContentResult(pdf.BinaryData, "application/pdf") With {.FileDownloadName = "google.pdf"}
	Catch e As Exception
		log.LogError(e, "Error while rendering pdf")
	End Try

	Return New OkObjectResult("OK")
End Function
$vbLabelText   $csharpLabel

已知问题

SVG 字体渲染在共享托管计划中不可用

一个限制是Azure 托管平台概述不支持服务器加载 SVG 字体,例如 Google 字体,在其更便宜的共享 web-app 层。 这是由于安全限制阻止访问 Windows GDI+ 图形对象。

我们建议使用IronPDF 的 Windows 或 Linux Docker 容器指南,或在需要最佳字体渲染的地方使用 Azure 上的 VPS。

Azure 免费托管速度慢

Azure 免费和共享层以及消耗计划不适合 PDF 渲染。 我们推荐 Azure B1 托管/高级计划,这是我们自己使用的。 HTML 到 PDF 的过程对于任何计算机来说都是重要的“工作”——类似于在自己的机器上打开和渲染网页。 使用了真实的浏览器引擎,因此需要相应地提供资源,并预期与类似性能的桌面机器类似的渲染时间。

创建工程支持请求票证

要创建请求票证,请参阅如何为 IronPDF 发起工程支持请求指南。

常见问题解答

如何在Azure上使用C#将HTML转换为PDF?

你可以使用IronPDF通过RenderHtmlAsPdf方法在你的Azure应用程序中将HTML转换为PDF。这使你能够在生成的PDF中保持HTML内容的格式。

在Azure Functions中部署PDF生成的最佳实践是什么?

在使用IronPDF部署Azure Functions中的PDF生成时,确保通过NuGet安装库,并在发布过程中避免选择“从打包文件运行”选项。这有助于防止常见的部署问题。

我可以在Azure上使用.NET 6与IronPDF吗?

是的,你可以在Azure上使用.NET 6与IronPDF。在你的.csproj文件中添加<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>,并创建一个runtimeconfig.template.json文件,将'System.Drawing.EnableUnixSupport'设置为true。

在Azure上获得最佳PDF渲染性能推荐使用哪种托管层?

为了在Azure上使用IronPDF获得最佳的PDF渲染性能,建议至少使用Azure基础B1托管层或升级到更高层,以适应高吞吐量系统。

在Azure的共享托管计划上使用IronPDF时有哪些限制?

是的,在Azure较便宜的共享托管计划上无法使用SVG字体渲染。为了更好的字体渲染和性能,使用Azure上的Windows或Linux Docker容器或VPS。

如何在Azure免费层上提高PDF渲染性能?

为了在超越Azure免费层限制的情况下提高PDF渲染性能,考虑升级到Azure基础B1托管层或更高。这有望缓解与免费层相关的性能瓶颈。

在Azure上使用IronPDF时有哪些已知问题?

已知问题包括共享托管计划上的SVG字体渲染限制和Azure免费层托管上的性能问题。使用Docker容器可以帮助缓解这些问题。

如何使用Docker运行用于PDF生成的Azure Functions?

你可以在Docker容器内部署Azure Functions,以在生成PDF时获得更好的性能和控制。有关详细信息,请参考Azure Docker Linux教程。

如何为Azure上的IronPDF寻求技术支持?

要为Azure上的IronPDF请求技术支持,你可以查看IronPDF网站上提供的“如何为IronPDF提交工程支持请求”指南。

在Azure上使用Docker时,可以访问SVG字体吗?

是的,使用Docker在Azure上可以访问SVG字体,从而提升使用IronPDF生成的PDF的渲染性能和质量。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布