html to pdf - 使用Wkhtmltopdf.exe - 實現,可導出需登錄的頁面


工作中一個比較常見的需求,要求將某個Url地址的內容轉換為PDF文件,在網上查找無數文章后,發現目前最簡單實用且效果很不錯的就是采用Wkhtmltopdf.exe程序來轉換。

安裝此程序后,有二種方式使用(本人的環境是Windows7,vs2010,wkhtmltopdf.exe是windows32位):

1.在VS2010中,可以使用Nuget搜索安裝Wkhtmltopdf插件到項目中,然后調用相應的方法,如下控制台程序代碼:

 1 static void Main(string[] args)
 2         {
 3             Console.InputEncoding = Encoding.UTF8;
 4 
 5             PdfConvert.Environment.Debug = true;
 6             PdfConvert.ConvertHtmlToPdf(new PdfDocument { Url = "http://www.codaxy.com" }, new PdfOutput
 7             {
 8                 OutputFilePath = "c:\\pdf\\codaxy.pdf"
 9             });
10             PdfConvert.ConvertHtmlToPdf(new PdfDocument { Url = "-", Html = "<html><h1>test</h1></html>" }, new PdfOutput
11             {
12                 OutputFilePath = "c:\\pdf\\inline.pdf"
13             });
14             PdfConvert.ConvertHtmlToPdf(new PdfDocument { Url = "-", Html = "<html><h1>測試</h1></html>" }, new PdfOutput
15             {
16                 OutputFilePath = "c:\\pdf\\inline_cht.pdf"
17             });
18         }

注意OutputFilePath 如果不輸入盤符,默認是生成在程序運行目錄下;需注意OutputFilePath 的輸出路徑寫入權限問題。

2.也可以編程執行Wkhtmltopdf.exe應用程序命令的方式來進行轉換。如下代碼(下面代碼從http://www.cnblogs.com/shanyou/archive/2012/09/07/2676026.html轉載):

 1  //Button的Click事件(把Url的網頁內容轉成PDF) 
 2         protected void btn_execute_Click(object sender, EventArgs e) 
 3         {
 4 
 5             //因為Web 是多線程環境,避免甲產生的文件被乙下載去,所以檔名都用唯一 
 6             string fileNameWithOutExtention = Guid.NewGuid().ToString();
 7 
 8             //執行wkhtmltopdf.exe 
 9             Process p = System.Diagnostics.Process.Start(@"D:\wkhtmltopdf\wkhtmltopdf.exe", @"http://msdn.microsoft.com/zh-cn D:\" + fileNameWithOutExtention + ".pdf");
10 
11             //若不加這一行,程序就會馬上執行下一句而抓不到文件發生意外:System.IO.FileNotFoundException: 找不到文件 ''。 
12             p.WaitForExit();
13 
14 
15             //把文件讀進文件流 
16             FileStream fs = new FileStream(@"D:\" + fileNameWithOutExtention + ".pdf", FileMode.Open); 
17             byte[] file = new byte[fs.Length]; 
18             fs.Read(file, 0, file.Length); 
19             fs.Close();
20 
21             //Response給客戶端下載 
22             Response.Clear(); 
23             Response.AddHeader("content-disposition", "attachment; filename=" + fileNameWithOutExtention + ".pdf");//強制下載 
24             Response.ContentType = "application/octet-stream"; 
25             Response.BinaryWrite(file);
26         } 

 

 

如果需要導出的頁面是需要Form認證才能訪問,則需要傳入登錄所需的信息,以下是cmd.exe中的示例(命令說明見各行的注釋):

1. C:\Program Files (x86)\wkhtmltopdf>wkhtmltopdf.exe           

2. --post ctl00$txtLoginAccount xxxxx@163.com //--post 表示Post方式提交數據,ctl00$txtLoginAccount 為登錄頁面輸入登錄帳號的控件的name名稱, xxxxx@163.com 為我輸入的帳號

3. --post ctl00$txtLoginPassword 123123 //同代碼行2,依次表示登錄頁面輸入密碼的控件name名稱,我輸入的密碼

4. --post ctl00$ContentPlaceHolder1$LoginOrRegisterUCL1$txtval dag2 //同代碼行2,依次表示登錄頁面輸入驗證碼控件name名稱,dag2 為我輸入的4位的驗證碼,如果還需要其它的登錄數據,按此方式傳入即可 

5. --ignore-load-errors  //官方的說法是,有時候登錄出錯加上這個命令就可以通過。

6. "http://www.xxxx.com/web/MyJobs/ViewApplicantInfoPage.aspx?resumeId=170&jobid=2&type=1&applicati
onId=119&userId=46&retUrl=http%3a%2f%2flocalhost%3a52226%2fweb%2fMyJobs%2fMyJobs
MainPage.aspx"         //要導出的需要form認證通過的頁面。

7. c:\pdf\222.pdf      //導出文件路徑,注意c:\pdf這個文件夾需要有寫入權限。
Loading pages (1/5)
Resolving links (2/5)
Counting pages (3/5)
Printing pages (5/5)
Done

如果需要在ASP.net頁面中實現上述功能,見以下代碼,代碼來源於以下地址:http://stackoverflow.com/questions/7019603/wkhtmltopdf-and-forms-based-authentication

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

  

Wkhtmltopdf相關資料(我的網盤):http://pan.baidu.com/s/1gdeJloJ


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM