代码之家  ›  专栏  ›  技术社区  ›  Juan Salvador Portugal

带有rotativa和空布局的图像头

  •  1
  • Juan Salvador Portugal  · 技术社区  · 6 年前

    我最近开始使用rotativa,这很神奇,但是今天,我尝试在pdf中添加一个头,它可以正常工作,但是头是使用 _layout.cshtml 当我试图用 Layout = null 或将标题呈现为 PartialView 而不是 View 它只是显示一个空页。

    我就是这样工作的

    public ActionResult Print(string CodigoDelfos, int Revision, string Versiones)
    {
        //Create the url for the header (Test action)
        UriBuilder u = new UriBuilder(Request.Url.AbsoluteUri)
        {
            Path = Url.Action("Test", "Home", null),
            Query = null
        };
    
        //Format the header to read Test.cshtml file
        string t = string.Format("--header-html {0}", u.Uri.ToString());
    
        //The action i'll use to create the pdf        
        return new Rotativa.ActionAsPdf(nameof(ImprimirDetalleDistribucion), new
        {
            CodigoDelfos,
            Revision,
            Versiones
        })
        {
            CustomSwitches = t
        };
    }
    

    然后 Test.cshtml 文件

    <div class="container">
        <img src="~/Content/Images/CabeceraGarantizados.jpg" class="img-fluid" />
    </div>
    

    如果我把它渲染成 View() (使用默认布局渲染)但如果我按 PartialView() pdf结果是 blank 就像我用 布局=空

    为什么会发生这种情况?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Juan Salvador Portugal    6 年前

    显然Rotary需要文件 DOCTYPE 定义

    解决方案只是添加 文档类型

    测试程序

    @{ Layout = null; }
    <!DOCTYPE html>
    <div class="container">
        <img src="~/Content/Images/CabeceraGarantizados.jpg" class="img-fluid" />
    </div>