Rotativa icon indicating copy to clipboard operation
Rotativa copied to clipboard

ActionAsPdf renders login view in pdf - Rotativa loses authentication

Open brianarmstrong-culturatech opened this issue 8 years ago • 7 comments

I would like to reference issue #57

I am using the Beta of Rotativa 1.7.1.
I am experiencing the issue described in #57 . I wrote code to perform .actionaspdf() in MVC3 and had absolutely no issues. Moved the exact code to an MVC5 app and the code breaks.
I am using Identity 2.0 in the MVC5 implementation and believe that may have something to do with it, but not sure.

The action that rotativa is passed will not be called unless the action is [AllowAnonymous] Once I set the action to allowanonymous, the pdf generates as it should, however within the action, the application no longer has the identity of the user that is generating the request.

If I remove the allowanonymous, the app believes I am not authenticated when I try to perform .actionaspdf() and sends me to the login page to authenticate . . . this in turn gets passed to Rotativa creating a PDF of the login page.

I was able to create a work around, by eliminating the action I was calling and use ViewasPDF(). (I did not feel comfortable setting my controls as AllowAnonymous to get .actionaspdf() to work)

use return new Rotativa.ViewAsPdf() { FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName, ViewName = "YourView", Model = YourModel };

or

var cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k].Value);

        return new Rotativa.ActionAsPdf("yourView")
        {
            FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName,
            Cookies = cookies
        };

scardenasojeda avatar Nov 15 '15 01:11 scardenasojeda

@salvaadicto thank you... That worked for me :)

Beslinda avatar Nov 16 '15 22:11 Beslinda

The problem with ViewAsPdf is that it renders no CSS at all, just plain text it seems. The problem with ActionAsPdf, for me, is that it results in errors like this: Failed loading page http://43.0 (sometimes it will work just to ignore this error with --load-error-handling ignore) also there are sometimes errors saying: QFont::setPixelSize: Pixel size <= 0

43.0 is obviously not a real address. Could it have something to do with glimpse or elmah? How to add the switch "--load-error-handling ignore" to wkhtmltopdf in Rotativa?

HugCoder avatar Dec 27 '15 22:12 HugCoder

@salvaadicto thank you , it worked but take too long time to download the file.

mghanii avatar Dec 05 '16 20:12 mghanii

ViewAsPdf solved my problem. ActionAsPdf generating error or generating login pdf.

        Dictionary<string, string> cookieCollection = new Dictionary<string, string>();
        foreach (var key in Request.Cookies.AllKeys)
        {
            cookieCollection.Add(key, Request.Cookies.Get(key).Value);
        }
        var abc = new ViewAsPdf("invoice", _customers)
        {
            FileName = "Name.pdf",
            Cookies = cookieCollection,
            FormsAuthenticationCookieName = FormsAuthentication.FormsCookieName
        };
        var byteArray = abc.BuildPdf(ControllerContext);
        var fileStream = new FileStream(Server.MapPath(subPath) + "/abc.pdf", FileMode.Create, FileAccess.Write);
        fileStream.Write(byteArray, 0, byteArray.Length);
        fileStream.Close();

anishmm avatar Mar 23 '17 07:03 anishmm

For anyone pulling their hair out trying to figure this one out as I was... I tried everything from passing in cookies with the DriverOptions to rendering the ViewAsPdf generated by Rotativa to a string (custom Razor extension) and converting that to a byte array - but just changing from ActionAsPdf to PartialViewAsPdf solved it for me.

For whatever reason PartialViewAsPdf contains the same (or better/improved...?) BuildPdf(ControllerContext) method as ActionAsPdf but ViewAsPdf does not.

kwinsor5 avatar May 30 '17 18:05 kwinsor5

var content = new Rotativa.ViewAsPdf("PDFContent", htmlContent); var binary = content.BuildPdf(this.ControllerContext); My problem is after call BuildPdf all sessions will be lost. Why and how to fix it. thnks

augustusle avatar Sep 15 '17 03:09 augustusle