Second call to dinktopdf pdf library fails

832 Views Asked by At

I'm working on a invoicing app built on asp.core/Razor (.net core 6). I have a form in the app that is used to create the invoice. After a a user fills all the required fields, he can open a modal that will show a preview of the invoice. If he is statisfied with how it looks, he can confirm that he wants to save it to pdf.

On save, I have a jquery function that makes an ajax call to the OnPost function of the razor page.

The Onpost function passes the form data along to a C# function that creates pdf doc and signs it. On the first attempt it works, but on the second attempt the process just hangs. I get no errors or exceptions

EDIT: on the c# method, I convert the html to pdf and it seems the process gets stuck during the conversion of html string to pdf bytes

2

There are 2 best solutions below

1
YourMomIsAHoua On

Edit: I was able to solve the issue. The source of the problem was a html to pdf library i used named dinktopdf. The issue i encounterd was mentioned in github:

https://github.com/rdvojmoc/DinkToPdf/issues/62

I implamented the solution that was mentioned there and it worked:

remove var converter = new SynchronizedConverter(new PdfTools()); and and replace it with dependency injection of 'IConverter' in your service: private readonly IConverter _converter; public NameOfModel (IConverter converter){this._converter = converter;}

Thank you all for your help.

NEW EDIT

First thing is to inject the service in Startup.cs Class

services.AddSingleton<IConverter>(new SynchronizedConverter(new PdfTools()));

And then constructor injection in the NameController.cs or Any other class you are working with :

 private readonly IConverter _converter;
  
public   NameController(IConverter converter)
{
            this._converter = converter;
        }

This should do the trick when your request is taking unexpectedly a long time or simply it just got hanged there

0
ezgienise On

1. Create a sealed class:

 public sealed class  Dummy
 {

   public static readonly IConverter converter = new SynchronizedConverter(new PdfTools());

   private Dummy() { }

 }

2. Call converter wherever you want:

//inside of your controller class
//inside of your method

...
            byte[] pdfBytes = Dummy.converter.Convert(doc);
...