Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf Log in to asp.net web app

Having a problem with wkhtmltopdf. I'm using it to take pdf snapshots of pages on a website that has a username/password page. When the .exe runs, I end up with a snapshot of the login page (running the exe from my own ASP.NET app).

Does anybody know how I would get wkhtmltopdf to log into the site so that it can access the page it needs to take a snapshot of?

wkhtmltopdf is installed in the program files directory on the server and is being called via:

public void HtmlToPdf(string website, string destinationFile)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "wkhtmltopdf.exe";
        startInfo.Arguments = website + " " + destinationFile;
        Process.Start(startInfo);
    }

Thanks! --Dan


THE ANSWER

I couldn't get the --cookie-jar method to work (see comments), but I did find another way to programmatically log in with the username/password in the querystring.

I pass the username/pw as params in my querystring and try to access the page I want with wkhtml. When the membership provider kicks me out to the login page, I access the params (which are stored in the url as the returnUrl param) via code-behind and authenticate myself. A simple response.redirect, and bingo -- I've got my PDF snapshot.

// Check to see if an outside program is trying
// to log in by passing creds in the querystring.
if (Request.QueryString["username"] != null) &&
    Request.QueryString["password"] != null))
{ 
    string user = Request.QueryString["username"];
    string pw   = Request.QueryString["password"];
    if (System.Web.Security.Membership.ValidateUser(user, pw))
    {
        // Create an authentication ticket for wkhtml session
        System.Web.Security.FormsAuthentication.SetAuthCookie(user, false);
        if (Request.QueryString["ReturnUrl"] != null)
        {
            Response.Redirect(Request.QueryString["ReturnUrl"]);
        }
    }
    else 
    {
        throw new Exception("You failed to log in.");
    }
}
like image 782
Daniel Szabo Avatar asked Jan 10 '11 20:01

Daniel Szabo


2 Answers

First, check the login form what post parameter it uses then try --post username xxx --post password xxx. Alternatively use wireshark and record the login process and see what parameters were posted.

Once u are logged in use --cookie-jar

See a better explanation here http://wkhtmltopdf.org/

getting wkhtmltopdf to convert a protected page can be tricky. Use also --extended-help to see other parameters u can use to login. e.g. if the site is protected by basic authentication it should be fairly easy with --user --password

like image 131
max Avatar answered Nov 05 '22 15:11

max


If anyone's still looking for an answer, I'll write up a brief summary of what I did to get it working.

First, inspect the page you want to log in to, for instance http:/www.example.com/login

Look for the form surrounding the username/password inputs. In my case, I was logging into a rails form, so I also needed the authenticity token. Once you have the name and values of the log in inputs, you can make the first wkhtmltoimage call like this:

wkhtmltoimage --cookie-jar my.jar --post username steve --post password iscool http://www.example.com/login dummy.jpg

In my case of the rails form, I needed to pass the auth_token as a post parameter as well. Then, just re use that cookie jar when accessing the main page you want to view:

wkhtmltoimage --cookie-jar my.jar http://example.com/myprofile screenshot.jpg

like image 30
Tim S Avatar answered Nov 05 '22 13:11

Tim S