9

I am attempting to get IronPDF working on my deployment of an ASP.NET Core 3.1 App Service. I am not using Azure Functions for any of this, just a regular endpoints on an Azure App Service -which, when a user calls it, the service generates and returns a generated PDF document.

When running the endpoint on localhost, it works perfectly- generating the report from the HTML passed into the method. However, once I deploy it to my Azure Web App Service, I am getting a 502 - Bad Gateway error, as attached (displayed in Swagger for neatness sake).

enter image description here

Controller:

[HttpPost]
[Route("[action]")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> AgencyDownload([FromBody] AgencyReportSubmissionDto filters)
{
  var user = await _userService.GetUserByIdAsync(HttpContext.User.GetUserId());

  // Generate the PDF
  var content = await _agencyReport.Generate(user, null, filters.FilterDate, filters.Content, filters.Type);

  // Return the PDF to the browser
  return new FileContentResult(content.BinaryData, "application/pdf") { FileDownloadName = "report.pdf" };
}

Service:

public async Task<PdfDocument> Generate(User user, byte[] letterhead, DateTimeOffset filterDate, string html, AgencyReportTypes reportType)
{
   var corporateIdentity = new CorporateIdentity()
   {
        PrimaryColor = "#000000",
        PrimaryTextColor = "#ffffff",
        SecondaryColor = "#ffffff"
   };

    // Inserts the HTML content (from form) into the HTML template
    var htmlContent = Template(corporateIdentity.PrimaryColor, corporateIdentity.PrimaryTextColor).Replace("{{HtmlContent}}", html);
        
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
    var convertedDate = TimeZoneInfo.ConvertTimeFromUtc(filterDate.UtcDateTime, tz);
    var Renderer = new ChromePdfRenderer();

    Renderer.RenderingOptions.Title = "Agency Report - for " + convertedDate.ToString("d MMMM yyyy");
    Renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;

    var doc = await Renderer.RenderHtmlAsPdfAsync(htmlContent);
    return doc;
}

Solution:

I noticed that if I performed a manual deployment to that app service, it was working, but when I was deploying from my pipeline- I had the error above.

So I went snooping around my pipelines and upon changing it to this, it worked.

            - task: AzureRmWebAppDeployment@4
              displayName: Deploy API Artifact
              inputs:
                ConnectionType: 'AzureRM'
                AzureSubscription: 'My-Azure-Subscription'
                enableCustomDeployment: true
                DeploymentType: 'zipDeploy'
                deployToSlotOrASE: true
                SlotName: 'development'
                AppType: 'webApp'
                WebAppName: 'my-api'
                Package: '$(Pipeline.Workspace)/**/API.zip'
                ResourceGroupName: 'MyResource'

the 'DeploymentType: 'zipDeploy'" was key.

Thanks to Alex Hanneman for pointing me in the right direction.

DiscoInferno
  • 142
  • 1
  • 10

3 Answers3

6

I am also using Azure App Service as an API, wrapping IronPDF. Upgrading to latest Iron PDF package also broke my app, returning a 502.

What I did to fix it is deploy the code using ZipDeploy and then set WEBSITE_RUN_FROM_PACKAGE to 0. This is also needed to get the Iron PDF log files to show up in Azure, as they recommend here: https://iron.helpscoutdocs.com/article/122-azure-log-files.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Disabling the 'Run from package file' option is also recommend on the main IronPDF website here: https://ironpdf.com/docs/questions/azure/#the-golden-rule – darren Dec 23 '21 at 03:43
  • This led me on the right track, though since I deploy my application using pipelines, I've had to simply add the attribute "deploymentMethod: 'zipDeploy'" to my artifact- in addition to changing my app service configuration as you've outlined. Thanks so much for the help! – DiscoInferno Jan 10 '22 at 08:37
  • @Alex Hanneman - I am also facing the same issue/error with Iron PDF, We have deployed our Containerized Asp.net Web API using Docker (Linux Image) in the Azure Kubernetes Service (AKS). Any guidance to resolve this issue with AKS? – Mukesh Arora Apr 25 '22 at 07:05
  • This is the best solution for Windows Container on Azure. Works for me. – Yogen Darji Mar 27 '23 at 06:34
  • Thanks Alex, this worked for me too with regard to not finding files I want to render as even though it automatically knows it's a ZIP deployment, it doesn't check the WEBSITE_RUN_FROM_PACKAGE and sets that to 1. Forcing it to choose zipDeploy doesn't change the environment variable. – Max Power Jul 28 '23 at 11:46
1

App Service runs your apps in a sandbox and most PDF libraries will fail. Looking at the IronPDF documentation, they say that you can run it in a VM or a container. Since you already are using App Service, simply package your app in a container, publish it to a container registry and configure App Service to run it.

CSharpRocks
  • 6,791
  • 1
  • 21
  • 27
  • Thanks for the feedback, I will need to do a bit of research into doing all of that but I'll give it a shot and get back to you. – DiscoInferno Dec 14 '21 at 06:28
  • I would suggest a Debian 10 Docker image as outline here: https://ironpdf.com/docs/questions/docker-linux/ – djrecipe Dec 14 '21 at 17:15
1

We are also facing the same issue with iron PDF but while changing the type of deployment to zip it's solves.

enter image description here

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35