1. First go to http://wkhtmltopdf.org/downloads.html to download the latest version of the installation package
2. Perform the installation complete
3. CMD command line run Wkhtmltopdf.exe program generate PDF
C:\Program Files\wkhtmltopdf\bin>wkhtmltopdf.exe--orientation Landscape--javascript c:\BPReport.html c:\BPReport_L.pdfLoading Pages (1/6) counting pages (2/ 6 ) Resolving links (4/6) Loading headers and footers (5/6 ) Printing pages (6/6) done
Parameters:
--orientation Landscape is a horizontal export
--javascript-delay 5000 is a time-lapse 5-second export that can be exported to a PDF when the page asynchronously loads data
Code call EXE
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;/*to reference the following namespaces*/ usingSystem.Diagnostics;usingSystem.IO; Public Partial class_default:system.web.ui.page {//Button's Click event (the URL's Web page content is converted to PDF) protected voidBtn_execute_click (Objectsender, EventArgs e) { //because the web is a multi-threaded environment, to avoid a file generated by B to download, so the file name with a unique stringFilenamewithoutextention =Guid.NewGuid (). ToString (); //Executive Wkhtmltopdf.exeProcess p = System.Diagnostics.Process.Start (@"D:\wkhtmltopdf\wkhtmltopdf.exe",@"HTTP://MSDN.MICROSOFT.COM/ZH-CN d:\"+ Filenamewithoutextention +". pdf"); //If you do not add this line, the program will immediately execute the next sentence without catching the file unexpected: System.IO.FileNotFoundException: File not found '. p.WaitForExit (); //read the file into the file streamFileStream fs =NewFileStream (@"D:\"+ Filenamewithoutextention +". pdf", FileMode.Open); byte[] File =New byte[FS. Length]; Fs. Read (file,0, file. Length); Fs. Close (); //response to client downloadResponse.Clear (); Response.AddHeader ("content-disposition","attachment; Filename="+ Filenamewithoutextention +". pdf");//Force DownloadResponse.ContentType ="Application/octet-stream"; Response.BinaryWrite (file); } }
If it is used. NET development, has been wrapped into NuGet suite, directly search Pechkin can be found, it has two versions: Pechkin for a single thread, such as to non-synchronous execution please choose Pechkin.synchronized.
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingPechkin;namespacehtml2pdf{classProgram {Static voidMain (string[] args) {//reading HTML varConfig =NewGlobalConfig (); varPechkin =NewSimplepechkin (config); Objectconfig OC=NewObjectconfig (); Oc. Setprintbackground (true) . Setloadimages (true) . Setpageuri ("http://news.google.com.tw/"); byte[] PDF =Pechkin. Convert (OC); File.writeallbytes ("d:\\temp\\google-news.pdf", pdf); //Homemade HTML stringHTML =@""; PDF=Pechkin. Convert (OC, HTML); File.writeallbytes ("d:\\temp\\myhtml.pdf", pdf); } }}
Wkhtmltopdf Generate PDF