MvcRazorToPdf – 图像无法渲染,MVC4
我一直试图渲染图像,但没有积极的结果。 是否需要添加任何具体内容才能使其正常工作。 我不包括渲染的其余部分,但在pdf中,图像丢失了。
我按照以下链接:
https://github.com/andyhutch77/MvcRazorToPdf
视图
@model Test.Models.PdfExample @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; var imagePath = Server.MapPath("~/Content/Images"); } // not rendering @**@
我可以看到
占据
width and height
但它没有显示其内部的图像。
我有同样的问题。 我的解决方案是:在控制器中尝试使用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视图。 记得设置边距。
然后在视图中执行以下操作:
因此,在您的情况下, 另一个问题可能是使用div属性,尤其是百分比。 我也有这个问题,最后我使用了TuesPechkin库来做一些事情,但我仍然使用MvcRazorToPdf来处理其他东西,例如带有动态内容的打印页面。 取决于目的。 尝试删除div属性。
我希望这能帮到您!
如果您想将HTML转换为PDF而没有任何问题,那么只需使用Pechkin这里是Fluent API:
byte[] pdf = new Pechkin.Synchronized.SynchronizedPechkin( new Pechkin.GlobalConfig()).Convert( new Pechkin.ObjectConfig() .SetLoadImages(true) //control image rendering .SetPrintBackground(true) .SetScreenMediaType(true) .SetCreateExternalLinks(true), html); //html is your html string
用更少的代码可以获得更好的结果。 它也可以在任何问题上运行IIS8,你还想要什么:-)仅供参考我不是Pechkin的提交者。
- “SaveChanges()”方法出错
- 如果没有将EnableOptimization设置为true,则JS包不会呈现
- 我应该将RouteParameter或UrlParameter用于Asp.NET web-api路由吗?
- 控制器名称的MVC路由约束
- ASP.NET WebApi – 一个控制器中的多个GET操作
- 如何实现MVC 4 Web App服务器端互斥锁
- 为什么WebApi控制器操作后没有人处理DbContext?
- 来自1 List 的MVC Multiple DropDownLists
- 如何返回false,如何创建将重定向到Login的自定义属性,类似于Authorize属性 – ASP.NET MVC