Open simpa151 opened 12 years ago
Could you provide details about the crash? Like the exception with the stack trace.
Also, your comment contradicts itself: first there's
byte[] buf = sc.Convert(new ObjectConfig(), html); <----THE CRASH OCCURES
and then
the thing is in the first run it reaches here... it reaches... SynchronizedPechkin sc = new SynchronizedPechkin and then it chrashes...
So in what line does it really crash? Or does it hang?
and it is just hanging
For the second question
is there any way i can insert page numbers like 1 of 3
Yes, there are two fields in ObjectConfig
:
public HeaderSettings Header { get; } // and
public HeaderSettings Footer { get; }
In them you can set header and footer of the every page with methods SetLeftText
/SetCenterText
/SetRightText
or SetTexts
. In the help for these methods you can find all replacements that will occur:
/// <summary>
/// Sets center text for the header/footer. Following replaces occur in this text:
/// * [page] Replaced by the number of the page currently being printed
/// * [frompage] Replaced by the number of the first page to be printed
/// * [topage] Replaced by the number of the last page to be printed
/// * [webpage] Replaced by the URL of the page being printed
/// * [section] Replaced by the name of the current section
/// * [subsection] Replaced by the name of the current subsection
/// * [date] Replaced by the current date in system local format
/// * [time] Replaced by the current time in system local format
/// </summary>
so you just include [page] of [topage]
string into the footer and here you go.
Also, are you really using SynchronizedPechkin
and not SimplePechkin
?
In the issue #4 (and in FAQ 4) we've found that SimplePechkin
doesn't work in web environments at all.
Yes i read about the Issue 4 and yes im using SynchronizedPechkin. The reason i called it a crash is it reaches the point in the code and then nothing happens... No errors occures it just hangs there the way i found out was logging where the algorithm reached in the code... And now a minute ago i just tested it and it worked...I didnt change any of the code...I have no idea why Yesterday it just hang... So what i ment was it hang first time here: byte[] buf = sc.Convert(new ObjectConfig(), html);
then if i tryed again in few seconds it hang here:
SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins(40, 20, 60, 50)) ...
I did this: ObjectConfig oc = new ObjectConfig(); oc.Footer.SetCenterText("__ [page] of [topage] __");
SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins(40, 20, 60, 50))
.SetDocumentTitle("CV").SetCopyCount(1).SetImageQuality(50)//.SetPageOffset(1)
.SetLosslessCompression(true).SetMaxImageDpi(120).SetOutlineGeneration(true).SetOutputDpi(1200).SetPaperOrientation(true)
.SetPaperSize(PaperKind.A4Rotated));
...
but nothing happens...
byte[] buf = sc.Convert(oc, html);
and it worked...
Okay, thank you. I think the underlying code was hanging for some reason, so any other request to the SynchronizedPechkin (like constructor) was queued, but never executed.
It's possible that SimplePechkin inside the Synchronized one gets broken due to bugs in my code =)
I'll investigate both issues today and report back what I've found.
Okay thank you so much...
Noticed: If the exec is not in the cache of the server it takes 14 seconds to generate the pdf... But if it is it generates immediately... Did you find anything about the issue??? :)
Hey, sorry, I got cought up in everything :( I'll reserach this bug very soon.
Hi, I have the same problem. Using SynchronizedPechkin, crash during byte[] buf = sc.Convert(new ObjectConfig(), html). I have found that the problem occures only on the server not on my PC. But strange is, that both computers are targeted to the same remote url. It occures only if there are used "large" images (good do print). If I use thumbnails, it works fine. The most strange thing is, that on my desktop it works fine, but not on the server.
Unfortunately I don't know how @simpa151 fixed this :( I'll get to it, but don't know in what time.
Just upload on server and let it be until tomorrow and then tomorrow try test or restart server....
Thank you!
This bug is related to Issue#12.
I investigated this a little bit further. If you provide an empty GlobalConfig and empty ObjectConfig there are usually no hangs and it works pretty flawlessly.
However, if you use the fluid interface and try to load images, or user other kinds of settings, it usually ends in a dead lock.
How is it going with this issue?
I'm currently using the wrapper myself, and the pdf is generated perfectly. But I'm having the same problems. In my .net mvc project I'm referencing a second project that I have created, that contains a small implementation of your Pechkin wrapper. This issue is kind of the only thing that is forcing me not to put this in production, since it is just not going to be safe enough.
Now what happens is when I stop the debug session, and then do a build after a change, Visual Studio just hangs, showing "build started". The asp.net development server serving the website that is using the wrapper, has to be stopped from the tray bar, in order to being able to build the project a second time. When this happens, it is also not possible to generate more pdf's.
I've tried to investigate by copying Pechkin code into separate classes, and see where it breaks. It seem to be in the SynchronizedDispatcherThread class, which is used by the SynchronizedPechkin class to delegate callbacks to the properties. Inside the Run method in SynchronizedDispatcherThread, the Exception block is catching some exception, and nothing happens after that. It just leaves the thread running. I don't know why it also makes the whole wrapper crash, and makes it unusable. I haven't figured out how to setup logging yet. Some examples would be great :-)
I've had no success in providing an empty GlobalConfig and empty ObjectConfig. Hokkaido, can you please give me some example code?
@mb280sel1985 - I found this issue when I was in debug mode with the hanging and having to stop the development server (IIS Express in my case) before it would let me rebuild and runwhen I first installed it from NuGet.
Somehow it had decided to dump all the DLL files into both the BIN and Root folder of the website. I resolved the issue by deleting the DLL's from the root folder (as theyre already in the Bin folder they arent needed at that location). Then rebuild and restart the project and it should be fine.
@bUKaneer No, this is most definitely not the issue, because in my case, there never were any DLL files inside the root folder.
@mb280sel1985 I think I might have just been lucky with my empty GlobalConfig and empty ObjectConfig. Maybe it's the HTML templates that also might trigger the issue.
I ultimately solved this issue by writing a windows service and implementing a simple task queue in my web application.
PDF creation requests are written to a database. A windows service polls this database and creates the PDFs. After that, the windows service updates the task status.
If I have time I might clean it up and provide the source for it, no promises though.
if @Kons in #12 is right that
the web server process doesn't shut down and the native libraries aren't unloaded, but the original managed static thread reference is discarded. At this point since the library has been initialized by the thread that was discarded in the process of recompilation, it will hang after being accessed again by the new thread assigned to the static variable after recompilation.
Then to fix it, we either need to develop some deploy sequence that will unload the libraries, or embed the libraries into the assembly and unpack them from the wrapper whenever it gets reset.
Because I'm unfamiliar with web development using .net (ok, I use Nancy, but no ASP or IIS), I'm unable to test deployment, so I'll add library bundling to my plans.
Hi Gmanny, Thanks for your dedication to this project. Your work has helped a lot of developers.
I think what you're proposing sounds a little over-complicated, if it will work at all. I think trying something like this may not be the most optimal use of your time.
It seems to me that this is a limitation in the native libraries which were never intended to be used in this fashion.
To web developers: To get this working in ASP.NET, compile the code as part of a console application and execute it from your web handler every time you need to get something converted. There should be plenty information about how to run another process/executable from your .NET application and pass data to it and back. This should work. I was going to try it, but unfortunately the task was deprioritized by my management and put on hold. If I'm allowed to resume it, I'll share the code.
This issue should be resolved with the following pull request:
Hi, I am trying to export the HTML formatted text to PDF(including images). It is exporting only first time, when i try to export after wards it is not showing any output(i am not even getting pdf window).
Please check my below code and let me know how to fix it.
litError.Text = string.Empty; try { ASPService.Employee obEmployee = obASPServiceClient.GetEmployeeDetails(radGrid.SelectedValue.ToString()); StringBuilder sb = new StringBuilder(); sb.Append("
#region Transform the HTML into PDF
var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true),sb.ToString());
#endregion
#region Return the pdf file
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + obEmployee.TGID +"_EK.pdf; size={0}", pdf.Length));
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
#endregion
}
catch (Exception ex)
{
litError.Text = string.Format("<div style='padding-top: 20px; color: #ff0000; width:100%;'><strong>Error message:</strong> {0}<hr /><strong>Stack trace:</strong> {1}", ex.Message, ex.StackTrace);
}
}
@sarinsoman From the code you've shared it looks like you're using my fork. I don't see anywhere you are disposing of the converter. When you don't dispose of the converter, the resources are not properly freed after the first use and so it will stop working properly on subsequent runs. Please wrap with a using statement like so:
using (var pechkin = Factory.Create(new GlobalConfig()))
{
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true),sb.ToString());
// Other code here
}
IPechkin pechkin = new SynchronizedPechkin(gc);//Pechkin.Synchronized ( (gc); var pdf = pechkin.Convert(oc, html);
at convert it gets hang. How to fix this issue?
if it is running localhost but it is not working server. You can restart application.if the error still persists You should IISRESET.
I was impressed by the code shared i downloaded opened it and run it it worked without any problems... Although the testapp was a forms app. I took this code and rewrote it for asp.net project. I ran it localhost and it worked perfectly... Now when i upload this to the server... The thread chrashes and it is just hanging... the error happens here:
SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins(40, 20, 60, 50)) .SetDocumentTitle("CV").SetCopyCount(1).SetImageQuality(50)//.SetPageOffset(1) .SetLosslessCompression(true).SetMaxImageDpi(120).SetOutlineGeneration(true).SetOutputDpi(1200).SetPaperOrientation(true) .SetPaperSize(PaperKind.A4Rotated));
byte[] buf = sc.Convert(new ObjectConfig(), html); <----THE CRASH OCCURES
the thing is in the first run it reaches here... but the next run it reaches... SynchronizedPechkin sc = new SynchronizedPechkin and then it chrashes...
please help... :) thank you in advance...
btw is there any way i can insert page numbers like 1 of 3