Wkhtmltopdf Login to asp.net web application

Problem with wkhtmltopdf. I use it to get pdf snapshots of pages on a website with username / password. When .exe starts, I get a snapshot of the login page (starting exe from my own ASP.NET application).

Does anyone know how I can get wkhtmltopdf to enter the site so that it can access the page on which it needs to take a picture?

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

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

Thanks! --Dan


ANSWER

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

I pass the username / pw as parameters in my query string and try to access the page I want with wkhtml. When the membership provider throws me to the login page, I access the parameters (which are stored in the URL as the returnUrl parameter) using the code and verify the authenticity. Simple response.redirect and bingo - I have 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."); } } 
+7
source share
4 answers

First check the login form, which recording option it uses, then try --post username xxx --post password xxx. Or use wireshark and write down the login process and see what parameters have been published.

Once you are logged in, use --cookie-jar

See the best explanation here http://wkhtmltopdf.org/

Getting wkhtmltopdf to convert a secure page can be difficult. Use also --extended-help to see other options that you can use to log in. for example, if the site is protected by basic authentication, this should be fairly easy with --user --password

+2
source

If someone is still looking for an answer, I will write a brief overview of what I did to make it work.

First check the page you want to access, for example http:/www.example.com/login

Look at the form surrounding the input for username and password. In my case, I entered the rails form, so I also need an authentication token. When you have the name and values โ€‹โ€‹of the log input, you can make the first call to whtmltoimage as follows:

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

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

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

0
source

Another way is to display the website view in an html string and temporarily save it in a local file. Then use wkhtmltopdf to convert this html file to PDF, then delete this local file. Thus, we do not need to handle MVC authentication.

0
source

I had a similar problem with my application that works with ASP.Net forms authentication. I had to pass cookies an authentication file, like this one, to make it work.

 --cookie <name> <value> 

The important point is that no additional cookies should be transmitted separately from the Auth file. Because some authentication cookies cause authentication to fail, and even if it is not, it slows down the wkhtml process, because wkhtml will have to process every cookie passed to it

0
source

All Articles