c# - 如何使用库 TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator 在 c# 中最终生成的 pdf 文档中下载嵌入式图像?
问题描述
我尝试使用TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator
C# 中的库生成 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.
这是输出:
请注意,图像不在输出 pdf 中。
如何让图像出现在输出 .pdf 中?
解决方案
有趣的是,我昨天遇到了完全相同的问题!有趣的是,这是怎么发生的,嗯?
我找到了解决我的问题的方法。有关详细信息,请在此处查看我的 stackoverflow 帖子:
https://stackoverflow.com/a/55286205/1289046
PS - 如果元级别的任何人都在看这个,我应该在另一个线程中链接到我的答案,还是应该在这里发布相同的答案?
推荐阅读
- vba - 创建 VBA 宏以创建箱线图
- dart - 在 Dart / Flutter 中比较 List 和 Value
- azure - 将 Thales payshield 9000 迁移到 Azure Key Vault
- php - Docker-Symfony-Mysql: SQLSTATE[HY000] [2002] 连接被拒绝
- json - 如何将带有单元 ML 测试示例的 json 文件加载到 jmeter http 请求有效负载中
- swift - RxSwift 缓冲区性能
- java - 遍历一个类型的所有代理
- javascript - WordPress 颜色选择器不适用于克隆 jQuery
- android - 用 Retrofit-gson 解析 JSON?
- javascript - `是什么意思!函数(...){...}(...);`?