Asp.net
wkhtmltopdf 登錄到 asp.net 網路應用程序
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."); } }
首先,檢查登錄表單它使用什麼 post 參數,然後嘗試 –post username xxx –post password xxx。或者使用wireshark並記錄登錄過程並查看發布了哪些參數。
一旦你登錄使用–cookie-jar
在此處查看更好的解釋http://wkhtmltopdf.org/
讓 wkhtmltopdf 轉換受保護的頁面可能很棘手。也可以使用 –extended-help 查看您可以用來登錄的其他參數。例如,如果站點受基本身份驗證保護,使用 –user –password 應該相當容易