wkhtmltopdf 和基于表单的身份验证

发布于 2024-11-29 03:21:18 字数 613 浏览 5 评论 0原文

我有一个使用基于表单的身份验证的应用程序。我正在尝试获取应用程序中页面的副本并使用 wkhtmltopdf 将其保存为 PDF。

wkhtmltopdf 已安装并且工作正常。我可以从应用程序内成功调用它来生成另一个网站的 PDF,无需身份验证(即 bbc.co.uk)。

我无法让它将用户名/密码传递给应用程序以生成所需页面的副本。相反,我得到了登录屏幕的 PDF 文件。

客户端和服务器都是Windows(分别是7和2008)。我正在使用命令:

wkhtmltopdf.exe --post userid=xxx --post pwd=yyy --ignore-load-errors http://url.com/blah/blah/ test.pdf

其中 xxx 是我的用户名,yyy 是我的密码。我已经浏览过登录页面的源代码,表单字段的 id 是 userid 和 pwd,所以我相信命令行中的 post 参数是正确的。

经过彻底的谷歌搜索后,我可以看到提到了 --cookie-jar 参数,但还没有设法弄清楚它的用法,或者即使它会有帮助。

谢谢

邓肯

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 source 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-12-06 03:21:19

如果您使用 ASP.NET 表单身份验证,则可以通过一系列 --cookie 参数将当前用户的身份验证 cookie 转发到 wkhtmltopdf。

注意:这不是从 .NET 应用程序启动 wkhtmltopdf.exe 的完整工作代码,而只是如何将 cookie 传递到 wkhtmltopdf 的示例。

有关从 .NET 启动 wkhtmltopdf.exe 的示例,请参阅:如何使用 wkhtmltopdf 将 html 作为字符串传递?

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);

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.

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);
半世蒼涼 2024-12-06 03:21:19

该问题现已解决。我使用的是 0.9.9 版本,但无法运行。当我转到 0.10.0 rc2 版本后,它就工作得很好。

如果其他人正在尝试同样的事情,我使用的代码行如上所述,我不需要 --cookie-jar 参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文