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"); } 
Test123 // not rendering @*Test123*@

我可以看到

占据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的提交者。