Htmlページ生成Pdf
22163 ワード
http://code.google.com/p/wkhtmltopdf/downloads/listでインストーラをダウンロードします.
1.参照の追加
参照の追加
2.方法
方法
3.呼び出し
よびだし
問題:utf-8符号化されていないWebページの変換で文字化けしが発生します.誰かがテストをしたことがあります:http://aiilive.blog.51cto.com/1925756/1340243
ネット上で他の変換方法を探しました.
http://www.html-to-pdf.net/ utf-8以外のWebページの変換もサポートされていません. http://www.51cto.com/へんかんふごう
http://www.winnovative-software.com/ テスト成功
コードは次のとおりです.
wnvhtmltopdf.dllを追加する必要があります
参照の追加
wnvhtmltopdfでHtmlをpdfに変換
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符号化されていないWebページの変換で文字化けしが発生します.誰かがテストをしたことがあります:http://aiilive.blog.51cto.com/1925756/1340243
ネット上で他の変換方法を探しました.
http://www.html-to-pdf.net/ utf-8以外のWebページの変換もサポートされていません. 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 }
wnvhtmltopdfでHtmlをpdfに変換