rdvojmoc / DinkToPdf

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

Subsequent PdfTools instances yield wrong PDF output #44

Open kierenj opened 6 years ago

kierenj commented 6 years ago

Hi! I'm trying to write some automated tests.

If I:

using (var tools = new PdfTools())
{
 var converter = new SynchronizedConverter(tools);
 // do the stuff, many times over
}

..then everything works fine.

However, if I:

using (var tools = new PdfTools())
{
 var converter = new SynchronizedConverter(tools);
 // do the stuff
}
using (var tools = new PdfTools())
{
 var converter = new SynchronizedConverter(tools);
 // do the stuff
}

The "stuff" being done is a randomly-named temp folder being set up, a HTML file and CSS file being copied in and the HTML being converted. I've put breakpoints and verified that for each conversion, the HTML and CSS are all valid. It's the PDF conversion bit which has the issue, for some reason!

..then only the first PDF is correct. Strangely, all of the subsequent PDFs simply have text in the source document dumped out.

The tests don't run in parallel, it seems that using a second (or rather, non-first) PdfTools instance is the issue.

Anything I can do to help diagnose?

carlos-eduardo-silva commented 6 years ago

I have this problem too!

mlynam commented 6 years ago

The PdfTools and Converter need to behave as singletons in your AppDomain.

kierenj commented 6 years ago

Yes, it certainly seems so. But I'd have hoped that there would be an exception, rather than randomly-corrupted PDF output. If this is indeed a design restriction, could I suggest a static flag somewhere, such that subsequent calls to the constructors throw a helpful error message?

mlynam commented 6 years ago

Yeah I'm not a fan of opinionated service lifetimes. I haven't dug into the code here to find out why this limitation exists, but rather I've just discovered this is the case via my own usage.

MagnusJohansson commented 5 years ago

Sadly, when trying to use singleton instances of a converter, and subsequent calls to Convert, DinkToPdf hangs: #62

mbenahssene commented 5 years ago

Same problem for me even with singleton. Is there any solution or update? Thank you.

serenata-alexander commented 2 years ago

Same for me in october 2022