Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Explanation:
HtmlToPdfFunction
which contains the Run
method annotated with [FunctionName("HtmlToPdfFunction")]
.HttpRequest
object and a logger log
.IronPdf.ChromePdfRenderer
is used to render the HTML as a PDF document.FileContentResult
containing the PDF, allowing it to be downloaded by the requester.Further Reading: How to Run & Deploy IronPDF .NET on Azure Function