Skip to content Skip to sidebar Skip to footer

Problem Loading Images Into Pdf Using Htmlrenderer.pdfsharp

This is a bit of a longshot, but maybe someone might have some ideas. For reference - see Images don't display in PDF, a similar question. I'm using the Htmlrenderer.PdfSharp libra

Solution 1:

After quite a bit of experimenting, I've found a solution. Here's a reference to the build that allows this solution to work, so be sure you have the right version:

https://github.com/ArthurHub/HTML-Renderer/pull/41

The solution is to load the image in question into a byte array, and then load the image directly into the HTML by using a base64 string representation of the image:

byte[] array = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"[YourImageFilePathHere]"));

This will take the file from a folder within your project's deployed path - in my case, I have a folder called images, within which my image in question is located.

Next, within the CSS (or, optionally, inline if you'd like):

htmlContent += " img.logo { width:110px;height:110px;content: url('data:image/jpeg;base64," + Convert.ToBase64String(array) +"')} ";

Lastly, within the HTML:

string imageUrl = "<imgclass=\"logo\"></img>";

And that's it! Worked perfectly for me.

Post a Comment for "Problem Loading Images Into Pdf Using Htmlrenderer.pdfsharp"