Calling wkhtmltopdf to generate PDF from the practice of adding up to a maximum of HTML foreigners, using wkhtmtopdf (GPL protocol) can save a lotProgramCodeFirst, go to the official website http://code.google.com/p/wkhtmltopdf/downloads/list
Find installer.exe to download
Wkhtmltopdf, an integrated EXE file (C ++), the Basic Call method is, wkhtmltow..exe http://passport.yupsky.com/ac
Count/register E: \ yupskyregister
You can test it on the command line first. You can use wkhtmltopdf -- help to query other requirements on the command line. If it is a very long page, you can use the command
Wkhtmltocmd.exe http://passport.yupsky.com/ac
Count/register E: \ yupskyregw.h -- Outline (-H is the default title, and -- Outline is the overview on the left of the PDF !) It can also be generated in batches, separated by spaces.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
/* Reference The following namespace */
Using system. diagnostics;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
// Click the button event (convert the URL's webpage content to PDF)
Protected void btn_execute_click (Object sender, eventargs E)
{
// Because the Web is a multi-threaded environment, files generated by Party A are not downloaded by Party B, so the file names are unique.
String filenamewithoutextention = guid. newguid (). tostring ();
// Execute wkhtmltocmd.exe
PROCESS p = system. Diagnostics. process. Start (@ "D: \ wkhtmltopdf \ wkhtmltocmd.exe", @ "http://msdn.microsoft.com/zh-cn D: \" + filenamewithoutextention + ". pdf ");
// If this line is not added, the program will immediately execute the next sentence and fail to catch the file. An error occurs: system. Io. filenotfoundexception: file not found ''.
P. waitforexit ();
// Read the file into the file stream
Filestream FS = new filestream (@ "D: \" + filenamewithoutextention + ". pdf", filemode. Open );
Byte [] file = new byte [fs. Length];
FS. Read (file, 0, file. Length );
FS. Close ();
// Response download to the client
Response. Clear ();
Response. addheader ("content-disposition", "attachment; filename =" + filenamewithoutextention + ". pdf"); // force download
Response. contenttype = "application/octet-stream ";
Response. binarywrite (File );
}
}
Two related projects were found on GitHub. The pechkin project does not need to install wkhtmltopdf separately, which is the. NET library.
C # wrapper around excellent wkhtmltopdf console utility https://github.com/codaxy/wkhtmltopdf
. Net wrapper for wkhtmltopdf static DLL. allows you to utilize full power of the Libra: https://github.com/gmanny/Pechkin