图像不渲染,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");
 }

<div style="width: 200%; height: 80px;">
    <div>
        <img alt="Test123" src="@imagePathimage.jpg" /> // not rendering
        @*<img alt="Test123" src="@Url.Content("~/Images/image.jpg")" />*@
    </div>
</div>

我可以看到<div>正在占用width and height,但它没有显示其中的图像。

我也遇到了同样的问题。我的解决方案是这样的:在控制器中尝试将完整的图像路径与服务器放在模型中。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视图。记住要设置页边距

然后在视图中执行如下操作:

<img src="@Model.ImgPath" />
那么,在您的情况下,另一个问题可能是使用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的提交者。

最新更新