利用WkHtmlToPdf,把H5 转成PDF

工具下载地址:

链接:https://pan.baidu.com/s/1TSq2WWZcvPwuIfPRHST-FA
提取码:wkx8

 原理:

通过IIS访问页面,利用WkHtmlToPdf.exe,把H5 转成PDF,方法如下

            string fileNameWithOutExtention = "test111";
            string AbsolutePath = Server.MapPath("~/Tools/wkhtmltopdf.exe");
            //执行wkhtmltopdf.exe
            //Process p = System.Diagnostics.Process.Start(@"D:\wkhtmltopdf\wkhtmltopdf.exe", @" http://localhost/JDBPMPDF/HtmlPage1.html D:\" + fileNameWithOutExtention + ".pdf");
            Process p = System.Diagnostics.Process.Start(AbsolutePath, @" http://localhost/JDBPMPDF/HtmlPage1.html D:\" + fileNameWithOutExtention + ".pdf");
            //若不加这一行,程序就会马上执行下一句而抓不到文件发生意外:System.IO.FileNotFoundException: 找不到文件 ''。
            p.WaitForExit();
            //把文件读进文件流
            FileStream fs = new FileStream(@"D:\" + fileNameWithOutExtention + ".pdf", FileMode.Open);
            byte[] file = new byte[fs.Length];
            fs.Read(file, 0, file.Length);
            fs.Close();

猜你喜欢

转载自www.cnblogs.com/ywkcode/p/10886393.html