Html pdf (C #)

Source: Internet
Author: User
Tags wkhtmltopdf

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

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.