利用WkHtmlToPdf,把H5 转成PDF

工具下载地址:

链接: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();

 

posted @ 2019-05-18 17:11  y_w_k  阅读(1256)  评论(0编辑  收藏  举报