Slow SelectPDF conversion after publish

758 Views Asked by At

I want to convert html code to pdf so I use SelectPDF library, so my code is:

               var converter = new HtmlToPdf();

               
                var today = DateTime.UtcNow;
                var fileName = $"test - {today}";

                var doc = converter.ConvertHtmlString(html);
                using var ms = new MemoryStream();
                ms.Position = 0;
                doc.Save(ms);

                var res = ms.ToArray();
                doc.Close();
                return File(res, "application/pdf", fileName);

I tested using localhost and everything works well, always do a fast conversion (not more than 5 seconds).

The problem starts when I publish on the server, after the method executed sometimes (not always) it returns an error 500

Failed to load resource: the server responded with a status of 500 ()
Message: "Conversion error: Navigation timeout."

Is it a way always to get a fast result? I know I can expand load time as:

converter.Options.MaxPageLoadTime = 120;

But I want to convert it fast, 2 minutes for a simple HTML to pdf conversion is to much

1

There are 1 best solutions below

0
On

If it works locally and you are getting a time-out on the server sometimes, it is likely that your Html contains a file reference (e.g. javascript, css or image) that is not available to the server at the time.

Make sure external references in your html that are always accessible to your server.