我在我的 Asp.net MVC5 项目中使用 MvcRazorToPdf 从模型创建 pdf。 这工作正常,但我想包含来自 base64 字符串的图像,因为我不想保存生成的图像。
System.Drawing.Image img = generator.GenerateImage();
string imageBase64Data = Convert.ToBase64String(Helper.ImageToByteArray(img));
string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data);
ViewBag.Image = imageDataURL;
return new PdfActionResult(myobject);
...
<img src="@ViewBag.Image" />
工作正常,如果在普通 View 中显示图像,但 pdf 不显示图像。
感谢您提供帮助或替代方案。
最佳答案
在 MvcRazorToPdf 中,您只能像这样使用图像文件的完整(本地)路径:
@model MvcRazorToPdfExample.Models.PdfExample
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_PdfLayout.cshtml";
var imagePath = Server.MapPath("~/Content/Images");
}
<img src="@imagePath\avatar.jpg" alt="mug shot" />
据我所知,不支持 Base64 图片。我从以下示例 View 中复制了上面的代码片段: https://github.com/andyhutch77/MvcRazorToPdf/blob/master/MvcRazorToPdfExample/Views/Pdf/Index.cshtml
关于来自 base64 的 C# Asp.net RazorPdf/iTextSharp 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28874363/