DiscoInferno
DiscoInferno

Reputation: 142

502 Error: Bad Gateway on Azure App Service with IronPDF

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.

Upvotes: 9

Views: 2269

Answers (3)

DEONTICSOFT
DEONTICSOFT

Reputation: 11

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

Upvotes: 1

Alex Hanneman
Alex Hanneman

Reputation: 76

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.

Upvotes: 6

user793891
user793891

Reputation:

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.

Upvotes: 0

Related Questions