HtmlRendererCore draws only half of the image when converting HTML into PDF

119 Views Asked by At

I'm using HtmlRendererCore.PdfSharpCore library to generate PDF files from HTML in my .NET 7 project. It creates the file correctly with all the text, tables and styles, except of the logo image, which is drawn only partially. Have anybody faced this issue? I was wondering if it could be some license related issue, but I didn't find any information whether free version of PdfSharpCore draws half images only.

Here is how I'm generating a file:

var stream = new MemoryStream();
PdfGenerator.GeneratePdf(htmlString, PageSize.A4, imageLoad: OnImageLoad).Save(stream);

And this is my OnImageLoad overload. I need it since I'm reading image as a stream from embedded resources:

private static void OnImageLoad(object sender, HtmlImageLoadEventArgs e)
{
    var logoStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("[resource path]");
    var image = XImage.FromStream(() => logoStream);
    e.Callback(image);
}

Here is resulting file generated [img] with half of the image drawn only (it should say "logo" in this example)

I've tried resizing the image with html attributes by setting bigger height, but it draws the same half of the image, just slightly bigger. I also tested if the image is full in logoStream by saving it to the file and it's fine there. How can I get my image displayed properly in a file?

P.S. I know that generating PDF from HTML is not the best option, but we need some free library to generate PDFs and it has been such a pain to find a good free one, which would also work on .NET Core

0

There are 0 best solutions below