我在我的Asp.net MVC5项目中使用MvcRazorToPdf从模型创建pdfs。这很好用,但是我想包含一个来自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" />如果在普通视图中显示图像,但pdf不显示图像,则工作正常。
感谢您的帮助或替代方案。
发布于 2015-06-09 02:19:30
也许我回答得太晚了。如果您需要基于base64字符串的图像以避免将图像存储在文件系统中,则可以编写一个返回图像文件并在img标记中引用该url的操作:
public ActionResult GetImage(/*probably some id*/)
{
System.Drawing.Image img = generator.GenerateImage();
ImageConverter converter = new ImageConverter();
var bytes = (byte[])converter.ConvertTo(img, typeof(byte[]));
return File(bytes, "image/jpg"); // if your image is jpg
}然后你的剃须刀看起来会像这样:
<img src='@Url.Action("GetImage", "Images", null, Request.Url.Scheme)' />注使用Request.Url.Scheme Url.Action重载非常重要,因为iTextSharp需要完全限定的URL。
发布于 2015-03-09 22:46:24
在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镜像。我将上面的代码片段从example视图复制到:https://github.com/andyhutch77/MvcRazorToPdf/blob/master/MvcRazorToPdfExample/Views/Pdf/Index.cshtml
https://stackoverflow.com/questions/28874363
复制相似问题