gpt4 book ai didi

c# - 如何在最终生成的 pdf 文档中使用库 TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator 下载嵌入式图像?

转载 作者:可可西里 更新时间:2023-11-01 14:59:28 33 4
gpt4 key购买 nike

我尝试使用 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>&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:image001.jpg@01D4F161.62577ED0">

<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: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.

这是输出:

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

请注意,图像不在输出 pdf 中。

如何让图像出现在输出 .pdf 中?

最佳答案

有趣的是,我昨天遇到了完全相同的问题!有趣的是,这是怎么发生的,嗯?

我找到了解决问题的办法。有关详细信息,请在此处查看我的 stackoverflow 帖子:

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

PS - 如果元级别的任何人都在看这个,我应该在另一个线程中链接到我的答案,还是应该在这里发布相同的答案?

关于c# - 如何在最终生成的 pdf 文档中使用库 TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator 下载嵌入式图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55274365/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com