1

We have a static method that returns pdf bytes but there is uncertain times the pdf it generates is blank/distorted. Trying to figure out if an instance of an html to PDF generator being inside a static method causes the output to fail by not being able to generate the expected content in the pdf.

Sample:

public class EvoPDFUtility
{
  public static byte[] ConvertHtmlToPdf(string html)
  {
     var htmlToPdfGenerator = new EvoPDF();
     return htmlToPdfGenerator.Convert(html);
  }
}

Is the instance inside the static method becomes shared to and the same everytime the static method is called?

johnsabre
  • 355
  • 2
  • 7

2 Answers2

2

No, htmpToPdfGenerator has local scope.

It is instantiated separately every time the static method is called and eligible for garbage collection as soon as that method exits.

Since each invocation of the static method references only a locally-scoped variable, the method is also thread safe.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • @johnsabre If you run a stress test where you convert the exact same HTML over and over with multiple conversions happening in parallel, do you always get the same results or does that sometimes fail? If it does, ensure you have proper exception handling in place so exceptions aren't going unnoticed. If a given HTML document either succeeds or fails under load, look at differences in the HTML to narrow down the root cause. A controlled load test is a good tool to resolve this. – Eric J. Jan 27 '23 at 21:52
2

Eric is right. It's not because it's a static function. It's more likely:

  1. Your HTML/CSS
  2. Fonts
  3. Possible image issues

Does it have issues generating the PDF several times against the same (good) HTML?

Luke101
  • 63,072
  • 85
  • 231
  • 359
  • Yes, first we thought it was the so we removed that in the html and it worked, then after a few more tries it generates blank again. Also when it breaks, other areas that also uses the same pdf library also breaks. – johnsabre Jan 27 '23 at 03:07