0

I have an HTML page rendered via ASP.NET. When I generate a PDF through HtmlRenderer.PdfSharp the images appear as a red X (missing image) in the PDF while the rest of the HTML appears properly. The images are not links, they are binary objects.

private void PdfFormat(string _body, ref MemoryStream _stream)
{
    Bitmap bitmap = new Bitmap(790, 1800);
    Graphics g = Graphics.FromImage(bitmap);
    XGraphics xg = XGraphics.FromGraphics(g, new XSize(bitmap.Width, bitmap.Height));

    TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer container = new TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer();
    container.SetHtml(_body);

    PdfDocument pdf = new PdfDocument();
    PdfPage page = new PdfPage();
    XImage img = XImage.FromGdiPlusImage(bitmap);
    pdf.Pages.Add(page);
    XGraphics xgr = XGraphics.FromPdfPage(pdf.Pages[0]);
    container.PerformLayout(xgr);
    container.PerformPaint(xgr);
    xgr.DrawImage(img, 0, 0);
    pdf.Save(_stream, false);
}
  • AIUI you have images in the HTML that are rendered as red X in the PDF. But you do not show the HTML, so how are we to know what is going wrong? The variables `g` and `xg` are not used in your code, so `bitmap` probably stays empty (and should not be rendered as a red X). Not enough code/information to replicate the issue. – I liked the old Stack Overflow Jun 19 '20 at 13:52

2 Answers2

0

the problem was in the decoded body not in the pdf generator. will close this issue and open a new one.

-1

You can use html2canvas, this will surely solve your problem.

const doc = new jsPDF();
        var canvasElement = document.createElement('canvas');
         html2canvas(this.$refs.content, { canvas: canvasElement 
           }).then(function (canvas) {
         const img = canvas.toDataURL("image/jpeg", 0.8);
         doc.addImage(img,'JPEG',30,20);
         doc.save("pdfName.pdf");
        });

Don't forgot to import:

import jsPDF from 'jspdf' 
import html2canvas from "html2canvas"
Chandra
  • 19
  • 5
  • my code is in C# and the image(s) are part of the encoded html body and they are not references, they are binary objects. – user3766852 Jun 19 '20 at 10:05