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> </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> </o:p></span></p>
<p>
<img width="643" height="345" style="width:6.7in;height:3.5916in" id="Picture_x0020_1"
src="cid:image001.jpg@01D4F161.62577ED0">
<span style="color:black"> <o:p></o:p></span></p>
<p><span style="color:black"><o:p> </o:p></span></p>
<p><span style="color:black">Thanks<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>
Here you can see the <img> tag which contains the src="cid:image001.jpg@01D4F161.62577ED0" 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:
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 ?