I have an application that uses forms based authentication. I'm trying to take a copy of a page within the application and save it to PDF using wkhtmltopdf.
wkhtmltopdf is installed and works fine. I can call it successfully from within the application to generate a PDF of another website without authentication (i.e. bbc.co.uk).
I cannot get it to pass username/password to the application in order to generate a copy of the desired page. Instead I get a PDF of the signon screen.
Both client and server are Windows (7 and 2008 respectively). I'm using the command:
wkhtmltopdf.exe --post userid=xxx --post pwd=yyy --ignore-load-errors http://url.com/blah/blah/ test.pdf
Where xxx is my username and yyy is my password. I've been through the so开发者_如何学Pythonurce of the logon page and the ids of the form fields are userid and pwd so I believe I have the post parameters in command line correct.
After thoroughly googling I can see mentions to a --cookie-jar parameter, but haven't managed to work out its usage, or even if it'll help.
thanks
Duncan
If you're using ASP.NET Forms Authentication, you can forward the current user's authentication cookies along to wkhtmltopdf via a series of --cookie
args. This is assuming that an authenticated use (from whom you can obtain auth cookies) is triggering the PDF printout.
Note: This is not complete working code for launching wkhtmltopdf.exe from a .NET app, but merely an example of how to pass along cookies to wkhtmltopdf.
For an example of launching wkhtmltopdf.exe from .NET, see: how to pass html as a string using wkhtmltopdf?
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "wkhtmltopdf.exe";
string cookieArgs = "";
var cookies = HttpContext.Current.Request.Cookies;
if (cookies != null)
{
var sb = new System.Text.StringBuilder();
// you probably only need the ".ASPXFORMSAUTH"
// and "ASP.NET_SessionId" cookies
// but I pass everything just in case
foreach (string key in cookies.AllKeys)
{
string value = cookies[key].Value;
sb.AppendFormat("--cookie {0} {1} ", key, value);
}
cookieArgs = sb.ToString();
}
psi.Arguments = urlToPrint + " -q " + cookieArgs + " -";
Process.Start(psi);
This issue is now resolved. I was using version 0.9.9 and couldn't get it to work. As soon as I moved to version 0.10.0 rc2 it worked fine.
If anyone else is trying the same thing, the line of code I used was as above, I didn't need the --cookie-jar parameter.
精彩评论