wkhtmltopdf 有问题。我正在使用它对具有用户名/密码页面的网站上的页面进行 pdf 快照。当 .exe 运行时,我会得到登录页面的快照(从我自己的 ASP.NET 应用程序运行 exe)。
有谁知道我将如何让 wkhtmltopdf 登录该站点,以便它可以访问它需要拍摄快照的页面?
wkhtmltopdf 安装在服务器的程序文件目录中,并通过以下方式调用:
public void HtmlToPdf(string website, string destinationFile)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "wkhtmltopdf.exe";
startInfo.Arguments = website + " " + destinationFile;
Process.Start(startInfo);
}
谢谢! - 担
答案
我无法使用 --cookie-jar 方法(请参阅评论),但我确实找到了另一种使用查询字符串中的用户名/密码以编程方式登录的方法。
我在查询字符串中将用户名/密码作为参数传递,并尝试使用 wkhtml 访问我想要的页面。当会员提供者将我踢出登录页面时,我通过代码隐藏访问参数(作为 returnUrl 参数存储在 url 中)并验证我自己。一个简单的 response.redirect 和宾果游戏——我有我的 PDF 快照。
// 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.");
}
}