- Steps to convert an Excel document to PDF in Azure Functions v4
- Steps to post the request to Azure Functions
Contact Support
Convert Excel document to PDF in Azure Functions v4
20 Feb 20255 minutes to read
Syncfusion® XlsIO is a .NET Excel library used to create, read, edit and convert Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can convert an Excel document to PDF in Azure Functions v4.
Steps to convert an Excel document to PDF in Azure Functions v4
Step 1: Create a new Azure Functions project.
Step 2: Name the project.
Step 3: Select the framework and click Create button.
Step 4: Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as a reference to your project from NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your applications to use our components.
Step 5: Include the following namespaces in the Function1.cs file.
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using Syncfusion.Pdf;
Step 6: Add the following code snippet in Run method of Function1 class to perform Excel to PDF conversion in Azure Functions and return the resultant PDF document to client end.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Gets the input Excel document as stream from request.
Stream excelStream = req.Content.ReadAsStreamAsync().Result;
//Load the stream into IWorkbook.
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Create the MemoryStream to save the converted PDF.
MemoryStream pdfStream = new MemoryStream();
//Save the converted PDF document to MemoryStream.
pdfDocument.Save(pdfStream);
pdfStream.Position = 0;
//Create the response to return.
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//Set the PDF document saved stream as content of response.
response.Content = new ByteArrayContent(pdfStream.ToArray());
//Set the contentDisposition as attachment.
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Sample.pdf"
};
//Set the content type as PDF document mime type.
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//Return the response with output PDF document stream.
return response;
}
Step 7: Right-click the project and select Publish option.
Step 8: Select the publish target as Azure.
Step 9: Select the Specific target as Azure Function App (Windows).
Step 10: Select the Create new button.
Step 11: Click the Create button to proceed with creation.
Step 12: Click the Finish button to finalize the Azure Function creation.
Step 13: Click Close button.
Step 14: Click the Publish button.
Step 15: Publish has been succeeded.
Step 16: Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Functions, to perform Excel to PDF conversion using the template Excel document). You will get the output PDF document as follows.
Steps to post the request to Azure Functions
Step 1: Create a console application to request the Azure Functions API.
Step 2: Add the following code snippet into Main method to post the request to Azure Functions with template Excel document and get the resultant PDF document.
//Reads the template Excel document.
FileStream excelStream = new FileStream("../../Sample.xlsx", FileMode.Open, FileAccess.Read);
excelStream.Position = 0;
//Saves the Excel document in memory stream.
MemoryStream inputStream = new MemoryStream();
excelStream.CopyTo(inputStream);
inputStream.Position = 0;
try
{
Console.WriteLine("Please enter your Azure Functions URL :");
string functionURL = Console.ReadLine();
//Create HttpWebRequest with hosted azure functions URL.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(functionURL);
//Set request method as POST
req.Method = "POST";
//Get the request stream to save the Excel document stream
Stream stream = req.GetRequestStream();
//Write the Excel document stream into request stream
stream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length);
//Gets the responce from the Azure Functions.
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//Saves the PDF stream.
FileStream outStream = File.Create("Sample.pdf");
res.GetResponseStream().CopyTo(outStream);
//Dispose the streams
inputStream.Dispose();
outStream.Dispose();
}
catch (Exception ex)
{
throw;
}
You can download a complete working sample from GitHub.
Click here to explore the rich set of Syncfusion® Excel library (XlsIO) features.
An online sample link to convert an Excel document to PDF in ASP.NET Core.