MvcRazorToPdf - 图像未呈现,MVC4 [英] MvcRazorToPdf - images not rendering, MVC4
问题描述
我一直在试图渲染图像,但没有积极的结果。有没有什么具体的我需要添加,使其工作。我不包括这使得罚款视图的休息,但在PDF,图像丢失。
I have been trying to render an image but with no positive result. Is there anything specific I need to add to make it work. I am not including the rest of the View which renders fine, but in the pdf, the image is missing.
我按照以下链接:结果
https://github.com/andyhutch77/MvcRazorToPdf
I have followed the below link:
https://github.com/andyhutch77/MvcRazorToPdf
查看
@model Test.Models.PdfExample
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
var imagePath = Server.MapPath("~/Content/Images");
}
<div style="width: 200%; height: 80px;">
<div>
<img alt="Test123" src="@imagePath\image.jpg" /> // not rendering
@*<img alt="Test123" src="@Url.Content("~/Images/image.jpg")" />*@
</div>
</div>
我可以看到&LT; DIV&GT;
正处在宽度和高度
,但它不显示它里面的形象。
I can see that the <div>
is occupying the width and height
but it is not showing the image inside of it.
推荐答案
我有同样的问题。
我的解决办法是这样的:在控制器尝试把该模型中的完整图像路径与使用Server.Mappath:
I had the same problem. My solution is this: in the controller try to put in the model the complete image path with Server.MapPath:
model.ImgPath = Server.MapPath("~/Content/Images/image.jpg");
然后用
byte[] pdfOutput = ControllerContext.GeneratePdf(
model,
"ImagePage",
(writer, document) =>
{
document.SetMargins(/*put your margins here*/);
document.SetPageSize(PageSize.A4);
document.NewPage();
});
在这里ImagePage是我用为目的的.cshtml视图。记住要设置页边距。
where ImagePage is the .cshtml view I used for the purpose. Remember to set the margins.
然后在视图中做这样的事:
Then in the view do something like this:
<img src="@Model.ImgPath" />
所以,你的情况,另一个问题也许div的使用性能,尤其是百分比。我有这个问题过了,最后我用TuesPechkin库一些事情,但我还是使用MvcRazorToPdf其他的东西像动态内容打印页数。取决于目的。尝试删除分区的属性。
So, in your case, another problem maybe the use of div properties, especially the percentage. I had this problem too, and in the end I used TuesPechkin library for some things, but I still use MvcRazorToPdf for other things like print pages with dynamic content. Depends on the purpose. Try to remove div properties.
我希望这会帮助你的!
这篇关于MvcRazorToPdf - 图像未呈现,MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!