wkhtmltopdfに問題があります。ユーザー名/パスワードページがあるWebサイトのページの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メソッドを機能させることができませんでした(コメントを参照)が、クエリ文字列にユーザー名/パスワードを使用してプログラムでログインする別の方法を見つけました。
ユーザー名/pwをクエリ文字列のparamsとして渡し、wkhtmlを使用して目的のページにアクセスしようとします。メンバーシッププロバイダーがログインページに私を追い出すとき、私はコードビハインドを介してパラメーター(URLにreturnUrlパラメーターとして保存されている)にアクセスし、自分自身を認証します。単純なresponse.redirectとbingo-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.");
}
}