gmanny / Pechkin

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

Error when using images #13

Closed jaredhall05 closed 12 years ago

jaredhall05 commented 12 years ago

I'm trying to use the following code:

GlobalConfig gc = new GlobalConfig();
gc.SetPaperSize(PaperKind.Letter);
if (!string.IsNullOrEmpty(requestData.PageOrientation) && requestData.PageOrientation.ToLower() == "landscape")
{
    gc.SetPaperOrientation(true);
}

ObjectConfig oc = new ObjectConfig();
oc.SetCreateExternalLinks(true)
    .SetFallbackEncoding(Encoding.ASCII)
    .SetLoadImages(true)
    .SetAllowLocalContent(true);

byte[] pdfBuf = new SimplePechkin(gc).Convert(oc, requestData.HtmlContent);
Stream pdfStream = new MemoryStream(pdfBuf);

with requestData.HtmlContent =

<html>
<body>
<h1>Hello world!</h1>
<img src='www.dogbreedinfo.com/images17/NorthernInuitDogFreya14wks.JPG' />
<a href='http://www.google.com'>Google</a>
</body>
</html>

Without the img tag it works fine, but with the image I get the following error:

Attempted to read or write protected memory.

I've tried a few different images all with the same problem. I even tried using the example and having it render http://google.com but that didn't help either.

If I use SynchronizedPechkin the error still occurs but is hidden away and I don't actually get the error message in visual studio

I'm using this in .NET 4.5 with WebApi in Visual Studio 2012.

gmanny commented 12 years ago

That's a stupid question to ask after thirteen days, but do you get the same error when you've got 'http://' in front of image url in img tag?

gmanny commented 12 years ago

Tested page

<html>
<body>
<h1>Hello world!</h1>
<img src='http://www.dogbreedinfo.com/images17/NorthernInuitDogFreya14wks.JPG' />
<a href='http://www.google.com'>Google</a>
</body>
</html>

with this code:

SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetPaperSize(PaperKind.Letter).SetPaperOrientation(true));

byte[] buf = sc.Convert(new ObjectConfig(), htmlText.Text);

it worked. Really don't think that those options are relevant. SimplePechkin also didn't throw any error (I was testing in WinForms).

The error could be thrown even in SynchronizedPechkin, if you first use SimplePechkin somewhere else in the code. Could you check for that too?

gmanny commented 12 years ago

When I removed http:// from link it didn't crash either, it just showed small empty square.

jaredhall05 commented 12 years ago

Sorry, forget to update this post, the problem went away after moving to a server. This only occurred when running locally.

Thanks for looking into this.

gmanny commented 12 years ago

That's weird. But not relevant now, I guess.

If you'll need to run the program locally, just reopen the ticket :)