How to generate HTML to PDF with .NET on Azure PDF

In this tutorial, we delve into HTML to PDF conversion using .NET on Azure, employing the IronPDF library. We begin by creating an Azure function project using the HTTP trigger in Visual Studio 2019, though any version can be utilized. The tutorial guides you through replacing the default Azure function code with HTML to PDF code, and installing the IronPDF library via the NuGet package manager. We build and run the project to ensure proper functionality, receiving a follow-up link as confirmation. Deployment on Azure follows, involving logging into your Azure account, selecting a subscription, and creating an app service instance with a B1 hosting plan. After setting up and publishing your project on Azure, you can access your deployed project through the Azure portal. The tutorial concludes by demonstrating how to perform an HTML to PDF conversion on Azure, highlighting the simplicity and effectiveness of using IronPDF. Support is readily available for further assistance.

Here is a sample code snippet on how to replace the default Azure Function code with HTML to PDF conversion code using IronPDF:

// Import necessary libraries
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using IronPdf;

public static class HtmlToPdfFunction
{
    [FunctionName("HtmlToPdfFunction")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        // This will read HTML content from the request body
        string htmlContent = new StreamReader(req.Body).ReadToEnd();

        // Create a PDF document from the HTML string
        var Renderer = new IronPdf.ChromePdfRenderer();
        var pdf = Renderer.RenderHtmlAsPdf(htmlContent);

        // Convert PDF to byte array
        byte[] pdfBytes = pdf.BinaryData;

        // Return the PDF as a response
        return new FileContentResult(pdfBytes, "application/pdf")
        {
            FileDownloadName = "converted.pdf"
        };
    }
}
// Import necessary libraries
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using IronPdf;

public static class HtmlToPdfFunction
{
    [FunctionName("HtmlToPdfFunction")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        // This will read HTML content from the request body
        string htmlContent = new StreamReader(req.Body).ReadToEnd();

        // Create a PDF document from the HTML string
        var Renderer = new IronPdf.ChromePdfRenderer();
        var pdf = Renderer.RenderHtmlAsPdf(htmlContent);

        // Convert PDF to byte array
        byte[] pdfBytes = pdf.BinaryData;

        // Return the PDF as a response
        return new FileContentResult(pdfBytes, "application/pdf")
        {
            FileDownloadName = "converted.pdf"
        };
    }
}
' Import necessary libraries
Imports System.IO
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Extensions.Logging
Imports IronPdf

Public Module HtmlToPdfFunction
	<FunctionName("HtmlToPdfFunction")>
	Public Function Run(<HttpTrigger(AuthorizationLevel.Function, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger) As IActionResult
		log.LogInformation("C# HTTP trigger function processed a request.")

		' This will read HTML content from the request body
		Dim htmlContent As String = (New StreamReader(req.Body)).ReadToEnd()

		' Create a PDF document from the HTML string
		Dim Renderer = New IronPdf.ChromePdfRenderer()
		Dim pdf = Renderer.RenderHtmlAsPdf(htmlContent)

		' Convert PDF to byte array
		Dim pdfBytes() As Byte = pdf.BinaryData

		' Return the PDF as a response
		Return New FileContentResult(pdfBytes, "application/pdf") With {.FileDownloadName = "converted.pdf"}
	End Function
End Module
$vbLabelText   $csharpLabel

Explanation:

  • We define a static class HtmlToPdfFunction which contains the Run method annotated with [FunctionName("HtmlToPdfFunction")].
  • This method is triggered by HTTP requests (either GET or POST) and receives the incoming HttpRequest object and a logger log.
  • It reads the HTML content from the request body to convert it into a PDF.
  • An instance of IronPdf.ChromePdfRenderer is used to render the HTML as a PDF document.
  • We convert the PDF document into a byte array to prepare it for the HTTP response.
  • Finally, it returns a FileContentResult containing the PDF, allowing it to be downloaded by the requester.

Further Reading: How to Run & Deploy IronPDF .NET on Azure Function

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Add Backgrounds & Foregrounds in PDFs
NEXT >
How to Generate PDFs with Async and Multithreading