我尝试使用C#中的库TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator
来生成pdf文档,但是主体HTML内部的内联图像未显示。有人有想法或解决方案该怎么做吗?
在电子邮件正文中添加的嵌入图片,例如 src =“ CID:” ,实际上这是在生成的PDF中不加载图像的问题。
这是我的示例输入:
<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.
这是我的代码片段:
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.
以下是输出:
This is the generated pdf result where you can see the image not loaded correctly
请注意,图像不在输出pdf中。
如何将图像包含在输出.pdf中?
答案 0 :(得分:0)
有趣的是,昨天我遇到了完全相同的问题!有趣的是怎么回事,是吗?
我找到了解决问题的方法。请在此处查看我的stackoverflow帖子以获取详细信息:
https://stackoverflow.com/a/55286205/1289046
PS-如果元级别上的任何人都在观看此内容,我应该在另一个线程中链接到我的答案,还是应该在此处发布相同的答案?