Rotativa ActionAsPdf() Very Slow
Asked Answered
F

2

4

Using Rotativa 1.6.4 from NuGet and have noticed the following issue using the code below.

ActionAsPdf hangs randomly for indeterminate amount of time.

Code below that is hanging:

   var pdfResult = new ActionAsPdf("Report", new {id = Request.Params["id"]})
    {
        Cookies = cookieCollection,
        FormsAuthenticationCookieName = FormsAuthentication.FormsCookieName,
        CustomSwitches = "--load-error-handling ignore"
    };

Background info that may help:

The customSwitches is in use to ignore a documented issue calling wkhtmltopdf.exe using the ActionAsPdf, but it does not suppress errors in the code only in the wkhtmltopdf call.

Observations, usage and testing:

It works but when running the application (whether or not stepping through code), it can be anywhere from 10 seconds up to about 4 minutes between hitting the pdfResult = new ActionAsPdf and finally entering into the "Report" action being called. Can't discern anything actually happening in the output window of Visual Studio, no errors are being thrown that I have found. Just random slow transition into the Reports() action.

I can run the Reports() action directly via URL and it never slows like this and is quite fast for PDF generation. I am running it using the ActionAsPdf to obtain the binary to save to file system and send via email, which is the prescribed method of doing so for this library.

The behavior exists on both a local Windows 10 dev box and a remote Server 2008R2 Test box. .Net 4.5.1 on both boxes, default IIS on each.

Questions I have:

Any idea on what might cause this slow down and how to remedy it?

Farceuse answered 29/9, 2015 at 21:39 Comment(0)
F
1

I ended up using UrlAsPdf() instead of ActionAsPdf() and it works. Seems there may be some issues with the ActionAsPdf() and I have filed a bug with Rotative project on GitHub. The ActionAsPdf() is still marked as beta, so hopefully it get's fixed in future versions or by the community.

Farceuse answered 12/10, 2015 at 21:26 Comment(5)
Did that fix the speed issue? Mine is still quite slow. It is taking several minutes on my development machine.Innumerable
The UrlAsPdf() has not had any slow issues across local dev, test, stage and prod environments I have used it on, where ActionAsPdf() has had issues across all environments, with the slow downs being seemingly random, as I've not yet pinpointed any conditions to reliably repeat it.Farceuse
Thanks for writing back! :) I ended up solving it a different way if you are curious: #35067691Innumerable
That very well may solve my issue as well, will be interested to try it soon. Thanks!Farceuse
I had a slowness issue as well and it turned out to be bootstrap in the view that was slowing it down. adding argument "--print-media-type" solved the issue. Now my view was very simple and it printed basically the same, I can see if you are trying to print rich content how that may not work.Ingravescent
A
0

In my case, I had to do few more tweaks along with using UrlAsPdf(). I have narrowed down the issue to the cookie collection that I was adding. So I tried just adding the cookie that I needed, and the issue was resolved. Following is the sample code that I have used.

        var report = new UrlAsPdf(url);

        Dictionary<string, string> cookieCollection = new Dictionary<string, string>();
        foreach (var key in Request.Cookies.AllKeys)
        {
           if (Crypto.Hash("_user").Equals(key))
           {
              cookieCollection.Add(key, Request.Cookies.Get(key).Value);
              break;
           }
        }
        report.Cookies = cookieCollection;
        report.FormsAuthenticationCookieName = FormsAuthentication.FormsCookieName;
Addis answered 8/11, 2020 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.