网页转为PDF的方法
下载第三方转换软件:
调用函数:
1 /// <summary>
2 /// HTML生成PDF
3 /// </summary>
4 /// <param name="url">地址</param>
5 /// <param name="path">PDF存放路径</param>
6 public static bool HtmlToPdf(string url, string path)
7 {
8 try
9 {
10 if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
11 return false;
12 Process p = new Process();
13 string str = System.Web.HttpContext.Current.Server.MapPath("wkhtmltopdf-0.8.3.exe");
14 if (!System.IO.File.Exists(dllstr))
15 return false;
16 p.StartInfo.FileName = dllstr;
17 p.StartInfo.Arguments = " \"" + url + "\" \"" + path + "\"";
18 p.StartInfo.UseShellExecute = false;
19 p.StartInfo.RedirectStandardInput = true;
20 p.StartInfo.RedirectStandardOutput = true;
21 p.StartInfo.RedirectStandardError = true;
22 p.StartInfo.CreateNoWindow = true;
23 p.Start();
24 System.Threading.Thread.Sleep(500);
25 return true;
26 }
27 catch (Exception ex)
28 {
29 HttpContext.Current.Response.Write(ex);
30 }
31 return false;
32 }
调用方法:
1 bool isDone = HtmlToPdf("http://www.baidu.com", "C:\\baidu.pdf");