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

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

问题描述

我使用这个 Rotativa 1.6.4 代码示例从我的 .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.

一旦我解决了身份验证问题,即使使用 CustomSwitches,PDF 生成速度也非常缓慢.(几分钟)

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

上面的代码实际上可能对您有用 - 它使用 Cookies 属性解决了身份验证问题,但对我来说太慢了.

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 问题的其他解决方案,但这些都不适合我.除了阅读代码之外的另一个优势...构建 pdb 文件,将其放入您的解决方案中并对其进行调试.它会揭示很多!我发现的一件事是 Rotativa 在幕后使用 wkhtmltopdf.exe.这使用 web kit 来呈现 html.此外,命令通常 会向 url 发出 http 请求.为什么?我们已经在服务器上了!这意味着我们将不得不重新进行身份验证并解释为什么我们有时会获得登录页面.复制 cookie 会有所帮助,但既然可以在线执行,为什么还要向自己发出 http 请求?

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 an http request to yourself when you can do it in-line?

我在源代码GetHtmlFromView 中找到了一个扩展方法,它无需单独发出http 请求即可生成视图html!是的!谁调用 GetHtmlFromView?为什么 ViewAsPdf 当然.所以这让我尝试了下面的代码,它有效且速度快!

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天全站免登陆