Wkhtmltopdf Generate PDF

Source: Internet
Author: User
Tags wkhtmltopdf

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.