Mehr Muhammad Hamza
Mehr Muhammad Hamza

Reputation: 127

HTML to PDF in C# on Azure Function (Linux) - IronPdf Permissions & Dependencies Issue

I’m setting up an Azure Function on Linux to convert HTML to PDF in C# using IronPdf. I installed the standard IronPdf NuGet package, and everything works fine locally, but after deployment, I keep getting errors about file system permissions and missing dependencies. I’ve tried reinstalling the package and redeploying it, but the issue persists. Since I’m committed to using IronPdf, I really need a solution that works within this library, not alternatives. Has anyone faced this before, and how did you resolve it?

Upvotes: 1

Views: 107

Answers (2)

chipego kalinda
chipego kalinda

Reputation: 59

I work at IronSoftware, and I appreciate the discussion here!

Pravallika KV has provided a great answer, and I’d like to add a few key points:

If you're running IronPDF on Azure Functions, be sure to check our official documentation for details on dependencies, file system permissions, and troubleshooting: 🔗 IronPDF on Azure Functions

Additionally, computational resources significantly affect performance in cloud environments. If you're facing issues after deployment, try increasing CPU and memory allocation, as PDF rendering is resource-intensive.

For real-time assistance, our 24/5 live chat support is available to help troubleshoot: 🔗 IronSoftware Live Support on the bottom right of the page you will see a chat button.

Hope this helps! Let me know if you have any other questions. 🚀

Upvotes: 1

Pravallika KV
Pravallika KV

Reputation: 8684

Follow the steps provided in official document to deploy the function with IronPdf to Linux Azure Function App.

I have deployed the function code to Azure function App (Linux) with App Service Plan (Premium).

Code Snippet:

Add License Key IronPdf.License.LicenseKey = "<License_key>"; and System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true); in the function code .

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log, ExecutionContext context)
{
    System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
    log.LogInformation("Entered PrintPdf API function...");
    
    IronPdf.License.LicenseKey = "<License_Key>";

    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;

    IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
    IronPdf.Installation.AutomaticallyDownloadNativeBinaries = true;
    IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

    try
    {
        log.LogInformation("About to render pdf...");
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello from Azure Functions!</h1><p>This is a sample PDF generated using IronPDF.</p></body></html>");

        var blobConnectionString = "<Connection_String>";  
        var containerName = "container1";
        var blobName = "google.pdf"; 

        var cloudStorageAccount = CloudStorageAccount.Parse(blobConnectionString);
        var blobClient = cloudStorageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference(containerName);
        await container.CreateIfNotExistsAsync();

        var blob = container.GetBlockBlobReference(blobName);
        using (var stream = new MemoryStream(pdf.BinaryData))
        {
            await blob.UploadFromStreamAsync(stream);
        }

        log.LogInformation("PDF uploaded to Blob Storage.");
        return new OkObjectResult($"PDF saved to Blob Storage with name: {blobName}");
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering pdf", e);
        return new OkObjectResult($"Error while rendering pdf: {e}");
    }
}

Add runtimeconfig.template.json: in the root directory of function App and add the below code.

{
  "configProperties": {
    "System.Drawing.EnableUnixSupport": true
  }
}

Able to run the function:

2025-02-20T14:15:16Z   [Information]   Successfully located 'IronPdfInterop' at '/home/site/wwwroot/bin'
2025-02-20T14:15:16Z   [Information]   Determining deployment commands for platform 'Debian' v12.0
2025-02-20T14:15:16Z   [Information]   Using deployment instructions for 'default' v
2025-02-20T14:15:16Z   [Information]   Attempting to load Unix library '/home/site/wwwroot/bin/IronPdfInterop.so'
2025-02-20T14:15:16Z   [Information]   Unix library '/home/site/wwwroot/bin/IronPdfInterop.so' load result 140329400401360
2025-02-20T14:15:16Z   [Information]   Attempting to load Unix library '/home/site/wwwroot/bin/IronPdfInterop.so'
2025-02-20T14:15:16Z   [Information]   Unix library '/home/site/wwwroot/bin/IronPdfInterop.so' load result 140329400401360
2025-02-20T14:15:16Z   [Information]   Successfully loaded native library from '/home/site/wwwroot/bin/IronPdfInterop.so'
2025-02-20T14:15:16Z   [Information]   Successfully loaded 'IronPdfInterop' version '2025.2.0.21' from '/home/site/wwwroot/bin'
2025-02-20T14:15:16Z   [Information]   Successfully deployed 'Pdfium' using '/home/site/wwwroot/bin'
2025-02-20T14:15:16Z   [Information]   Creating new document collection resource
2025-02-20T14:15:16Z   [Information]   Successfully initialized local pdf client
2025-02-20T14:15:16Z   [Information]   14:15:16 (140329165182656): Opening document from job result 0
2025-02-20T14:15:16Z   [Information]   14:15:16 (140329165182656): [JRC]Releasing job result 0
2025-02-20T14:15:16Z   [Information]   14:15:16 (140329165182656): PdfDocumentFactory created document 0x7fa0fc137820 from 13278 bytes
2025-02-20T14:15:16Z   [Information]   14:15:16 (140329165182656): Document opened from pointer
2025-02-20T14:15:16Z   [Information]   14:15:16 (140329165182656): Storing new document 0
2025-02-20T14:15:16Z   [Information]   Created document 0 from job result 0
2025-02-20T14:15:16Z   [Information]   Document 0 has 1 references remaining (+1)
2025-02-20T14:15:17Z   [Information]   14:15:16 (140609581610688): Generating bytes for document 0x7fa0fc137820
2025-02-20T14:15:17Z   [Information]   14:15:16 (140609581610688): Successfully generated 15985 bytes for document 0x7fa0fc137820
2025-02-20T14:15:17Z   [Information]   PDF uploaded to Blob Storage.
2025-02-20T14:15:17Z   [Information]   Executed 'PrintPdf' (Succeeded, Id=b022ffd6-6e79-4386-a944-e0053ffdded6, Duration=53267ms)

Converted html to pdf and uploaded to Blob Container:

enter image description here

Storage Container:

enter image description here

Generated pdf file:

enter image description here

Upvotes: 2

Related Questions