PDF to Image conversion in Azure functions using Syncfusion PDF Viewer server library
The Syncfusion PDF Viewer in ASP.NET Core (Essential JS 2) is a modern enterprise UI toolkit that has been built from the ground up to be lightweight, responsive, modular, and touch-friendly. It is also available in other frameworks such as JavaScript, Angular, ASP.NET MVC, and React.
Refer to the following UG link for getting started with the PDF Viewer control.
https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/getting-started/
The PDF Viewer server library allows the PDF document pages to be exported as raster images.
Steps to convert the PDF pages to Images in Azure functions programmatically
- Create an Azure Functions project.
Azure Functions project
- Select the framework to Azure functions V2(.NET Core) and select the HTTP triggers as follows.
Select framework and HTTP triggers
- Install the Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows NuGet package as a reference to the application from NuGet.org.
Install the PDF Viewer NuGet package
For Linux and OSX operating systems, use the following corresponding libraries:
- Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux
- Syncfusion.EJ2.PdfViewer.AspNet.Core.OSX
- Include the following namespaces in the Function1.cs file.
using Syncfusion.EJ2.PdfViewer; using System.Drawing;
- Add the following code in the Run method of Function1 class to convert the PDF document pages into images in Azure functions.
public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { log.LogInformation("C# HTTP trigger function processed a request."); PdfRenderer PdfRenderer = new PdfRenderer(); //Load the PDF document. string FilePath = Path.Combine(context.FunctionAppDirectory, "PDF_Succinctly.pdf"); //Refer to the path using the ReferencePath API. PdfRenderer.ReferencePath = context.FunctionAppDirectory + "\\bin\\"; PdfRenderer.Load(FilePath); //Exports the PDF document pages into images. Bitmap BitmapImage = PdfRenderer.ExportAsImage(5); MemoryStream MemoryStream = new MemoryStream(); BitmapImage.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Png); string Base64String = "data:image/png; base64," + Convert.ToBase64String(MemoryStream.ToArray()); //Save the exported image in a disk. return new OkObjectResult(Base64String); }
- Right-click the project and select Publish. Choose the Target as Azure.
Select target for publish
- Then, create a new Azure Function in the Publish window.
Create a new Azure Function
- Create an App Service using the Azure subscription and select a hosting plan.
Create App service and hosting plan
- Configure the hosting plan.
Configure hosting plan
- After creating the profile, click the Publish button.
Publish the app
- Now, go to the Azure portal and select App Services. After running the service, click the Advanced Tools in the created function app and then click Go to display the Kudu console.
Advanced Tools
- Select the CMD from the dropdown and then navigate inside to the (site/wwwroot) folder to see the published items.
Published items
- You can test or run the output of the function by following these steps.
- Navigate to the Functions.
Functions
- Choose the function and click on it.
Choose the function
- Go to Code + Test and click Test/Run.
Test the code
- Then, click the Run button.
Run the code
- Now, copy the base64 string in the HTTP response content.
Copy the base64 string
- Paste the content in the browser to view the generated output.
Output Image
A complete working sample to convert the PDF page to an image in the Azure functions can be downloaded from the PDFToImage_AzureFunctions.zip.
Refer to this link to explore a rich set of PDF Viewer features.
An online demo link to view PDF documents using Syncfusion PDF Viewer.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.