为什么总是Rotativa我的生成登录页?为什么慢? [英] Why does Rotativa always generate my login page? Why is it slow?

查看:154
本文介绍了为什么总是Rotativa我的生成登录页?为什么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就是用这个Rotativa 1.6.4 code为例,生成在我的.NET MVC应用5页的PDF文件。

I was using this Rotativa 1.6.4 code example to generate a PDF from a page in my .NET MVC 5 app.

public ActionResult PrintIndex()
{
    var a = new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
    a.Cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k].Value);
    a.FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName;
    a.CustomSwitches = "--load-error-handling ignore";
    return a;
}

public ActionResult Index(string name)
{
    ViewBag.Message = string.Format("Hello {0} to ASP.NET MVC!", name);

    return View();
}

这不是打印索引页,而是被打印我的登录页面。

It was not printing the Index page, but instead was printing my login page.

在我固定的身份验证问题,生成PDF甚至与 CustomSwitches 极其缓慢。 (几分钟)

Once I fixed the authentication issue, PDF generation was extremely slow even with CustomSwitches. (Several minutes)

以上code实际上可能为你工作 - 它使用饼干属性得到周围的认证问题,但它的方式对我来说太缓慢

The above code might actually work for you - it got around the authentication issue using the Cookies property, but it was way too slow for me.

我如何打印安全页面,以及快快行动吧?

How do I print a secure page as well as do it quickly?

推荐答案

我这个奋斗了大概8小时,我张贴我自己的解决方案,部分原因是由于自我参照,而且因为在堆栈溢出没有很好的答案。

I struggled with this for probably 8 hours and I am posting my own solution partly as a self reference, but also because there was no good answer in stack overflow.

这是在github上开源的。我试过很多其他的解决方案,人们说要用 UrlAsPdf 和其他解决方案从GitHub的问题,但是这一切都不为我工作。除了看报code另一个优点......构建 PDB 文件,它折腾到您的解决方案和调试进去。这将揭示很多!有一件事是,我发现Rotativa使用 wkhtmltopdf.exe 在幕后。它使用的网络工具包来渲染HTML。另外,命令的一般的发出HTTP请求的URL。为什么?我们已经在服务器上!这意味着我们将不得不重新进行身份验证并解释了为什么我们有时会登录页面。复制饼干会有帮助,但为什么主动要求自己,当你可以在网上做呢?

It's open source on github. I tried lots of other solutions where people said to use UrlAsPdf and other solutions from github issues, but none of this worked for me. Another advantage besides reading the code... Build the pdb file, toss it into your solution and debug into it. It will reveal a lot! One thing I found is that Rotativa uses wkhtmltopdf.exe under the covers. This uses web kit to render the html. Also the command usually makes an http request to a url. Why? We are already on the server! That means we would have to re-authenticate and explains why we can sometimes get the Login page. Copying cookies will help, but why make a request to yourself when you can do it in-line?

我在源代码中发现的扩展方法 GetHtmlFromView 产生未做单独的HTTP请求查看HTML!是!谁在叫 GetHtmlFromView ?为什么 ViewAsPdf 当然。因此,这导致我​​试试下面的code,它的工作原理是快!

I found an extension method in the source GetHtmlFromView which generates the view html without making a separate http request! YES! Who calls GetHtmlFromView? Why ViewAsPdf of course. So this lead me to try the below code, which works and is fast!

// ViewAsPdf calls Rotativa.Extensions.ControllerContextExtensions.GetHtmlFromView
// Which generates the HTML inline instead of making a separate http request which CallDriver (wkhtmltopdf.exe) does.
var a = new ViewAsPdf();
a.ViewName = "Index";
a.Model = _service.GetMyViewModel(id);
var pdfBytes = a.BuildPdf(ControllerContext);

// Optionally save the PDF to server in a proper IIS location.
var fileName = string.Format("my_file_{0}.pdf", id);
var path = Server.MapPath("~/App_Data/" + fileName);
System.IO.File.WriteAllBytes(path, pdfBytes);

// return ActionResult
MemoryStream ms = new MemoryStream(pdfBytes);
return new FileStreamResult(ms, "application/pdf");

这篇关于为什么总是Rotativa我的生成登录页?为什么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆