工具下載地址:
鏈接:https://pan.baidu.com/s/1TSq2WWZcvPwuIfPRHST-FA
提取碼:wkx8
原理:
通過IIS訪問頁面,利用WkHtmlToPdf.exe,把H5 轉成PDF,方法如下
1 string fileNameWithOutExtention = "test111"; 2 string AbsolutePath = Server.MapPath("~/Tools/wkhtmltopdf.exe"); 3 //執行wkhtmltopdf.exe 4 //Process p = System.Diagnostics.Process.Start(@"D:\wkhtmltopdf\wkhtmltopdf.exe", @"http://localhost/JDBPMPDF/HtmlPage1.html D:\" + fileNameWithOutExtention + ".pdf"); 5 Process p = System.Diagnostics.Process.Start(AbsolutePath, @"http://localhost/JDBPMPDF/HtmlPage1.html D:\" + fileNameWithOutExtention + ".pdf"); 6 //若不加這一行,程序就會馬上執行下一句而抓不到文件發生意外:System.IO.FileNotFoundException: 找不到文件 ''。 7 p.WaitForExit(); 8 //把文件讀進文件流 9 FileStream fs = new FileStream(@"D:\" + fileNameWithOutExtention + ".pdf", FileMode.Open); 10 byte[] file = new byte[fs.Length]; 11 fs.Read(file, 0, file.Length); 12 fs.Close();