DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

Subsequent PdfTools instances yield wrong PDF output

Open kierenj opened this issue 5 years ago • 7 comments

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?

kierenj avatar Aug 01 '18 15:08 kierenj

I have this problem too!

carlos-eduardo-silva avatar Aug 16 '18 20:08 carlos-eduardo-silva

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

mlynam avatar Aug 24 '18 13:08 mlynam

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?

kierenj avatar Aug 24 '18 14:08 kierenj

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.

mlynam avatar Aug 24 '18 14:08 mlynam

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

MagnusJohansson avatar Jan 13 '19 15:01 MagnusJohansson

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

mbenahssene avatar Sep 09 '19 19:09 mbenahssene

Same for me in october 2022

serenata-alexander avatar Oct 24 '22 14:10 serenata-alexander