在验证墙后生成PDF [英] Generate PDF Behind Authentication Wall
问题描述
我试图使用WKHTMLTOPDF生成一个PDF,要求我先登录。在互联网上已经有一些,但我似乎无法得到我的工作。
I'm trying to generate a PDF using WKHTMLTOPDF that requires me to first log in. There's some on this on the internet already but I can't seem to get mine working. I'm in Terminal - nothing fancy.
我尝试过(其他很多东西):
I've tried (among a whole lot of other stuff):
/usr/bin/wkhtmltopdf --post username=myusername --post password=mypassword "URL to Generate" test.pdf
/usr/bin/wkhtmltopdf --username myusername --password mypassword "URL to Generate" test.pdf
/usr/bin/wkhtmltopdf --cookie-jar my.jar --post username=myusername --post password=mypassword "URL to Generate Cookie For"
用户名和密码都是 id
以及表单上输入字段的名称
。
username and password are both the id
and the name
of the input fields on the form. I am getting the my.jar file to show up, but nothing is written to it.
具体问题:
- 我应该在任何地方指定登录页面和/或表单操作吗?
- 在各种地方提到了--cookie-jar参数需要和其他)。如果有必要,它是如何工作的?我创建了my.jar文件,但是如何再次使用它?引用:
http://code.google.com/p/wkhtmltopdf/issues/detail?id=356
EDIT:
当然有人成功了吗?一个展示示例的好方法是,如果有人愿意在需要登录凭据的一些热门网站上工作,以消除潜在变量。
Surely someone has done this successfully? A good way to showcase an example might if someone is willing to get it to work on some popular website that requires login credentials to eliminate a potential variable.
推荐答案
我想我要登录的形式太复杂了。它是安全的,设置三个cookie,重定向两次,并发布用户名和密码之外的其他变量,其中一个需要一个cookie值(我甚至尝试连接的值到post变量,但没有运气)。这可能是一个非常罕见的问题 - 决不是WKHTMLTOPDF的错误。
I think the form I'm trying to log in to is too complex. It's secure, sets three cookies, redirects twice, and posts a number of other variables outside of the username and password, one of which requires a cookie value (I even tried concatenating the value into the post variable, but no luck). This is probably a pretty rare issue - by no means the fault of WKHTMLTOPDF.
我绕过使用CURL登录并将页面写入本地文件,然后运行WKHTMLTOPDF反对。
I wound up using CURL to log in and write the page to a local file, then ran WKHTMLTOPDF against that. Definitely a solid work around for anyone else having a similar issue.
编辑:CURL,如果感兴趣:
CURL, if interested:
curl_setopt($ch, CURLOPT_HEADER, 1); # Change to 1 to see WTF
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
这篇关于在验证墙后生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!