rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.08k stars 415 forks source link

Loading unmanaged library in .net core 2 and then converting more than one document fails #10

Closed timfisher closed 7 years ago

timfisher commented 7 years ago

I used the load unmanaged library code in a new service for each document converted. Even using freelibrary() on the libwkhtmltox.dll afterwards doesn't help as the second time it tries it fails. Is there something that could be modified with the singleton in the startup.cs?

timfisher commented 7 years ago

The only way I could get this to work was by using the singleton, without the singleton it fails. Hope this helps someone

rdvojmoc commented 7 years ago

You are correct. This detail is also written in README

timfisher commented 7 years ago

In the README you say this first:

Synchronised converter

Use this converter in multi threaded applications and web servers. Conversion tasks are saved to blocking collection and executed on a single thread.

var converter = new SynchronizedConverter(new PdfTools());

Then you mention DI, and there is no mention it is mandatory. Also the basic converter does not work at all in this way.

rdvojmoc commented 7 years ago

Changed README to be more clear on how converter should be registered in DI.

sundarSDI commented 6 years ago

Facing the same Issue, Find my DI in Startup.cs

// In Start Up Class services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

It works for the first request, from next it hangs and no response.

Thanks in Advance.

teocomi commented 6 years ago

A part from declaring the singleton in Startup.cs as indicated in the README, I had to add this to my controller:

       private readonly IConverter _converter;

       public MyController(IConverter converter)
       {
           _converter = converter;
       }

       private byte[] GeneratePdf(string html)
        {

            var doc = new HtmlToPdfDocument()
            {
              ...
            };
            var pdf = _converter.Convert(doc);
            return pdf;
        }