5

I've written a PDF generation API that uses dinktopdf to convert some templated HTML to a byte array. This all works fine on my local machine but when I deploy to my azure web application the API only works once. When I try it a second time I get the following message and a 502 error:

The specified CGI application encountered an error and the server terminated the process.

Here's a stripped down version of my code that still presents the same error:

static IPdfConverter pdfConverter = new SynchronizedConverter(new PdfTools());

public static byte[] BuildPdf(string html)
{
    return pdfConverter.Convert(new HtmlToPdfDocument()
    {
        Objects =
        {
            new ObjectSettings
            {
                HtmlContent = html
            }
        }
    });
}

I've also tried using IronPDF to do the HTML to PDF conversion and gotten the same exact issue (works perfectly on local machine but only once on Azure deployment before giving consistent 502 errors).

user3053470
  • 241
  • 4
  • 9

2 Answers2

7

If you initialize converter with var converter = new SynchronizedConverter(new PdfTools());´, remove it, and just injectIConverterin your service. I tried and this approach is working, after all, we already register the converter, we don't need to create an instance using thenew` keyword. My app is not on Azure, but I have the same problem, the converter hangs after the first call, for that reason I decide to write down this comment.

Alexander
  • 151
  • 3
  • 5
  • 2
    OP already figured out that his code is ok but issue was related to infrastructure limitations. If you think that your answer is relevant then please add extra explanation. Also you can use formatting to separate you text from code. – Maxim Sagaydachny Dec 11 '19 at 13:33
  • @MaximSagaydachny - Thank you for your recommendations, you are absolutely right. This is my second comment and I must learn, how to do it properly. About the problem may be my comment is not for here but in my case and other cases that I found, this is still an issue, the converter hangs out, after the first call. My app is not deployed yet and have this issue, but after I make that change, everything works as I expect. – Alexander Dec 12 '19 at 08:00
6

Update: Problem was solved by changing the Azure App Service Plan to Basic rather than free (PDF generation requires at minimum the Basic plan apparently).

user3053470
  • 241
  • 4
  • 9