I have tried to generate the pdf document using the library TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator in C#, but the inline images inside the body HTML do not show up. Does anyone have an idea or solution how to do that?

Embedded image added in email body text with e.g. src="CID:", which is actually a problem in not load an image in generated PDF.

Here is my sample input:

<b>Dell</b><hr style='background-color: #000000 !important;color: #000000 !important;border: solid 1px #000000 !important;'/><br/><table border='0'><col width='100'><col><tr><td style='font-weight:bold;'>From:</td><td>dipak patel</td></tr><tr><td style='font-weight:bold;'>Sent:</td><td>Friday, April 12, 2019 06:58 PM</td></tr><tr><td style='font-weight:bold;'>To:</td><td>dipak patel</td></tr><tr><td style='font-weight:bold;'>Subject:</td><td>Test Embedded image</td></tr></table><html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head>

</head>
<body lang="EN-IN" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p><span style="color:black">My test no is HO284848<o:p></o:p></span></p>
<p><span style="color:black"><o:p>&nbsp;</o:p></span></p>
<p><span style="color:black">This is my embedded image.<o:p></o:p></span></p>
<p><span style="color:black">e.g.<o:p></o:p></span></p>
<p><span style="color:black"><o:p>&nbsp;</o:p></span></p>
<p>

<img width="643" height="345" style="width:6.7in;height:3.5916in" id="Picture_x0020_1" 
src="cid:[email protected]">

<span style="color:black">&nbsp;<o:p></o:p></span></p>
<p><span style="color:black"><o:p>&nbsp;</o:p></span></p>
<p><span style="color:black">Thanks<o:p></o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</body>
</html>

Here you can see the <img> tag which contains the src="cid:[email protected]" which is embedded image and not loaded in PDF.

Here is my code fragment:

public static string GeneratePDFWithHTML(string BodyHTMLText, string SavedFilePath)
{
    //PdfSharp.Pdf.PdfDocument pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(BodyHTMLText, PdfSharp.PageSize.Letter, 20, null, null, OnImageLoad);
    PdfSharp.Pdf.PdfDocument pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(BodyHTMLText, PdfSharp.PageSize.Letter);
    ////pdf.AddPage()
    pdf.Save(SavedFilePath);

    return "";
}

private static void OnImageLoad(object sender, HtmlImageLoadEventArgs e)
{
    using (var client = new WebClient())
    {
        var url = e.Src;
        //e.Attributes.Add("width", "90");
        if (!e.Src.StartsWith("http://") && !e.Src.StartsWith("https://"))
        {
            //url = Properties.Settings.Default.BaseUrl.TrimEnd('/') + e.Src;
            url = "http://localhost:58909/content/" + e.Src;
        }
        using (var stream = new MemoryStream(client.DownloadData(url)))
        {
            //e.Callback(PdfSharp.Drawing.XImage.FromStream(stream));
        }
    }
}

I have commented out the code in function GeneratePDFWithHTML which i actually needs with OnImageLoad event to load the embedded images.

Here is the output:

Generated PDF example with embedded image not loaded in final output

This is the generated pdf result where you can see the image not loaded correctly

Note that the image is not in the output pdf.

How do I get the image to be in the output .pdf ?

1

There are 1 best solutions below

0
On

Interestingly, I had exactly the same problem yesterday! Funny how that happens, eh?

I found a solution to my problem. Please see my stackoverflow post here for details:

https://stackoverflow.com/a/55286205/1289046

PS - if anyone on a meta-level is watching this, should I be linking to my answer in another thread, or should I be posting that same answer here instead?