在http://code.google.com/p/wkhtmltopdf/downloads/list下载安装程序。
1.添加引用
1 using System.Diagnostics;
2.方法
1 /// <summary>
2 /// 把对应的网页转化成Pdf文件
3 /// </summary>
4 /// <param name="Url">网页网址</param>
5 /// <param name="Path"></param>
6 /// <returns>成功返回true失败返回false</returns>
7 public static bool HtmlTOPdf(string Url, string Path)
8 {
9 if (!string.IsNullOrEmpty(Url)&&!string.IsNullOrEmpty(Path))
10 {
11 try
12 {
13 Process p = new Process();
14 p.StartInfo.FileName = "cmd.exe";
15 p.StartInfo.WorkingDirectory = "C:\\WINDOWS\\system32";
16 p.StartInfo.UseShellExecute = false;
17 p.StartInfo.RedirectStandardInput = true;
18 p.StartInfo.RedirectStandardOutput = true;
19 p.StartInfo.RedirectStandardError = true;
20 p.StartInfo.CreateNoWindow = true;
21 p.Start();
22 string cmd = "C:/Users/PC/Desktop/wkhtmltopdf/wkhtmltopdf.exe" + " " + Url + " " + Path + " ";
23 p.StandardInput.WriteLine(cmd);
24 p.WaitForExit(6000);
25 p.Close();
26 return true;
27 }
28 catch (Exception)
29 {
30 return false;
31 }
32 }
33 else
34 {
35 return false;
36 }
37 }
3.调用
1 Html2Pdf.HtmlToPdf("http://www.cnblogs.com/", "d:/cnblogs.pdf");
出现的问题:非utf-8编码的网页转换会出现乱码。有人做过测试:http://aiilive.blog.51cto.com/1925756/1340243
在网上找了一些其他的转换方法:
http://www.html-to-pdf.net/ 也不支持非utf-8的网页转换,测试网站 http://www.51cto.com/ 转换乱码
http://www.winnovative-software.com/ 测试成功
下面是代码:
需要添加wnvhtmltopdf.dll
1 using Winnovative;
1 //create a PDF document 2 Document document = new Document(); 3 4 //optional settings for the PDF document like margins, compression level, 5 //security options, viewer preferences, document information, etc 6 document.CompressionLevel = PdfCompressionLevel.Normal; 7 document.Margins = new Margins(10, 10, 0, 0); 8 //document.Security.CanPrint = true; 9 //document.Security.UserPassword = ""; 10 document.ViewerPreferences.HideToolbar = false; 11 12 // set if the images are compressed in PDF with JPEG to reduce the PDF document size 13 document.JpegCompressionEnabled = true; 14 15 //Add a first page to the document. The next pages will inherit the settings from this page 16 PdfPage page = document.Pages.AddNewPage(PdfPageSize.A4, new Margins(10, 10, 0, 0), PdfPageOrientation.Portrait); 17 18 // the code below can be used to create a page with default settings A4, document margins inherited, portrait orientation 19 //PdfPage page = document.Pages.AddNewPage(); 20 21 // add a font to the document that can be used for the texts elements 22 PdfFont font = document.Fonts.Add(new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, 23 System.Drawing.GraphicsUnit.Point)); 24 25 // the result of adding an element to a PDF page 26 AddElementResult addResult; 27 28 // Get the specified location and size of the rendered content 29 // A negative value for width and height means to auto determine 30 // The auto determined width is the available width in the PDF page 31 // and the auto determined height is the height necessary to render all the content 32 float xLocation = 0; 33 float yLocation = 0; 34 float width = 0; 35 float height = 0; 36 37 // convert HTML to PDF 38 HtmlToPdfElement htmlToPdfElement; 39 40 // convert a URL to PDF 41 string urlToConvert = "http://www.51cto.com/"; 42 43 htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height, urlToConvert); 44 45 //optional settings for the HTML to PDF converter 46 htmlToPdfElement.FitWidth = true;//合适的宽度 47 htmlToPdfElement.EmbedFonts = false;//嵌入字体呈现PDF文档中真正的类型 48 htmlToPdfElement.LiveUrlsEnabled = false;//网页链接是否可用 49 htmlToPdfElement.JavaScriptEnabled = true;//在转换期间是否启用JavaScript和其他客户端脚本 50 htmlToPdfElement.PdfBookmarkOptions.HtmlElementSelectors = null;//Bookmark H1 and H2 HTML tags 51 52 // add theHTML to PDF converter element to page 53 addResult = page.AddElement(htmlToPdfElement); 54 55 try 56 { 57 // get the PDF document bytes 58 byte[] pdfBytes = document.Save(); 59 60 // send the generated PDF document to client browser 61 62 // get the object representing the HTTP response to browser 63 HttpResponse httpResponse = HttpContext.Current.Response; 64 65 // add the Content-Type and Content-Disposition HTTP headers 66 httpResponse.AddHeader("Content-Type", "application/pdf"); 67 httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename=51cto.pdf; size={0}", pdfBytes.Length.ToString())); 68 69 // write the PDF document bytes as attachment to HTTP response 70 httpResponse.BinaryWrite(pdfBytes); 71 72 // Note: it is important to end the response, otherwise the ASP.NET 73 // web page will render its content to PDF document stream 74 httpResponse.End(); 75 } 76 finally 77 { 78 // close the PDF document to release the resources 79 document.Close(); 80 }