gmanny / Pechkin

.NET Wrapper for WkHtmlToPdf static DLL. Allows you to utilize full power of the library.
400 stars 128 forks source link

Problem running Convert() multiple times #4

Closed raydk closed 12 years ago

raydk commented 12 years ago

Hi,

First of all, I would like to say that it's a very nice library you have created. Everything works fine, except for one thing. I have created a simple application where I try to write a PDF upon a button click. The first time the PDF is created fast and with no problem, but the application just hangs forever without any errors after postback when i try to click the button again. Do you have any idea what the problem could be?

This is my sample code:

protected void Button1Click(object sender, EventArgs e) { string targetFolderPath = String.Format("~/Resources/ProjectFiles/Project{0}/Letters/{1}/", SessionWrapper.Current.currentProjectID, LetterType); Response.Clear(); string pdfPath = String.Format(targetFolderPath + "/{0:yyyy-MM-dd_hh-mm-ss}helloworld.pdf",DateTime.Now); byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("

Hello world!

"); ByteArrayToFile(@Server.MapPath(pdfPath), pdfBuf); }

raydk commented 12 years ago

Oops I didn't realise that the comment box supported HTML tags :D

raydk commented 12 years ago

I forgot to mention that the application hangs on this line on the second run.

byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("Hello world!");

gmanny commented 12 years ago

Hello, thank you!

It's strange, because I designed, coded and tested Pechkin to be able to run multiple times. Could you please provide details about OS on which you're running the test?

And also, in this repo there's project PechkinTestApp, could you please check, if it works for you? (You'll need to check out all the code and open the solution, for the project to be compiled correctly.)

raydk commented 12 years ago

Thanks for your fast reply.

I'm running Windows 7 Pro using Visual Web Developer 2010.

I also think it's strange that it just hangs without any error. At first I thought it was because the FileStream wasn't disposed or something like that,but later on I realised that it didn't reach to the writing part before it started to hang.

I will try to download the PechkinTestApp project and see if it causes same issues.

gmanny commented 12 years ago

This test app really does just what you do, and it runs fine for me on Windows 7 Pro x64. Even with modification to use SimplePechkin.

So I need more details to reproduce the bug.

Also, you could try SynchronizedPechkin insted of Simple one, but within your one UI thread SimplePechkin is supposed to be fine.

gmanny commented 12 years ago

In fact, if it's web app (and not WinForms, as I thought before), you should use SynchronizedPechkin, since for every request there's new thread in web apps.

raydk commented 12 years ago

Well the test app runs fine, also multiple times. Perhaps there are some difference because my application is a webapp.

gmanny commented 12 years ago

Yeah, see comment above :)

raydk commented 12 years ago

Allright, I will try to use the SynchronizedPechkin, So what difference does it make if there's a new thread for every request? :)

gmanny commented 12 years ago

The undelying library (WkHtmlToPDF) isn't thread friendly, everything inside it gets messed up, when you work with it from multiple threads.

It really happens to all the code written without threading in mind :)

So SynchronizedPechkin creates one thread to work with the library and tunnels all the calls to this thread. This way it keeps the lib safe.

raydk commented 12 years ago

I see, I didn't give that any thought before I jumped into the simple app. But you have saved me from a big headache because my application works now with the SynchronizedPechkin library! :D

Thank you very much for your help and keep up the good work! This library have saved me from a lot of trouble if I had to use iTextSharp or something similar.