Articles in this section
Category / Section

How to resolve the issue “Access to the path is denied” during the PDF to image conversion in Azure functions?

3 mins read

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. Refer to the following KB link for the PDF to Image conversion in Azure functions using the Syncfusion PDF Viewer server library.

https://www.syncfusion.com/kb/12610/pdf-to-image-conversion-in-azure-functions-using-syncfusion-pdf-viewer-server-library

You might have faced an issue “Access to the path is denied” if the PDFIUM assembly is not mapped to the exact location. The procedure to resolve this issue is discussed here.

Steps to resolve the issue “Access to the path is denied” during the PDF to Image conversion in the Azure functions:

  1. Download the PDFIUM assembly from the following link.

PDFIUM assembly: https://www.syncfusion.com/downloads/support/directtrac/general/ze/pdfium956869027.zip

  1. Copy the local (x64 and x86) folders and keep them in the sample’s root directory.
  2. Right-click on the pdfium.dll and then choose the “Copy to Output Directory” property from the property window and set “Copy always” as its value. This setting will ship the pdfium.dll assembly to the published location.

Properties of pdfium.dll

Properties of pdfium.dll

  1. Similarly, set the “Copy to Output Directory” property to “Copy always” for the input PDF file being used for the conversion. This setting will ship the PDF document to the published location.
  2. Pass the ExecutionContext in the parameter as shown in the following highlighted code.
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log, ExecutionContext context)
    
  1. Refer to the context path in the ReferencePath API as highlighted, so that you can export the PDF to an image correctly within the Azure functions.
    [FunctionName("Function1")]
    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);
        //Export 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);
    }
    
  1. Build and publish the application.

To ensure the changes:

You can also see the published items by clicking the Advanced Tools in the created function app and then click Go to display the Kudu console.

Advanced Tools

Advanced Tools

Select the CMD from the dropdown and then navigate inside to the (site/wwwroot) folder.

Published items

Published items

Note:

Make sure that the x64 and x86 folders are available in the location where the pdfium.dll must be shipped.

 

Following the above steps will resolve the issue “Access to the path is denied” during the PDF to Image conversion in Azure functions.

A complete working sample to convert the PDF page to an image in the Azure functions can be downloaded from the following link.

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDFToImage_AzureFunctions-1764243556

Refer to this link to explore a rich set of PDF Viewer features.

An online demo link to view PDF documents using the Syncfusion PDF Viewer.

Note:

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied