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 library to create PDF files from HTML content. Everything works perfectly, with the sole exception of the images. They simply display a big red box.
The PDF works fine when running locally in debug mode, but not when deployed on the server. I've a small bit of extra information that might help - when executing the command to create the PDF:
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
On the server, I get a few of the following errors:
Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll
Running locally I don't get those errors. I have a feeling it has something to do with the .NET engine not being able to properly resolve the image URL. That being said, the image URL is fully-qualified. It is, however, behind HTTPS, if that helps.
I'm unable to go any further with my debugging. If anyone has any ideas, I'd love to hear them - even if it is just more places I can try hunting for clues.
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 = "<img class=\"logo\"></img>";
And that's it! Worked perfectly for me.