ASP. NET C # export PDF based on HTML page

Source: Internet
Author: User
Tags wkhtmltopdf

In the Venus sourcing system, a new export PDF function is added. The entire feature uses third-party software wkhtmltopdf (download) official website https://wkhtmltopdf.org/offers more version download

He can convert HTML page to PDF, the software is simply incredible-incredible, the function is too powerful.

Because, I have a HTML, reference a lot of CSS, and the page is basically JS dynamically generated, have been worried that the wkhtmltopdf generated PDF will be a blank

Not quite, after the conversion, those CSS and JS are not "distorted".

Here's the code that uses C # to generate the HTML PDF:

           stringURL ="http://www.dotnetcms.org/About.aspx";
string pdf = "c:\pdf\bin\wkhtmltopdf.exe"
stringfilename =Guid.NewGuid (). ToString (); stringPdfpath = filename +". pdf"; Process P= System.Diagnostics.Process.Start (pdf, URL +" \""+ Server.MapPath (Pdfpath) +"\""); p.WaitForExit (); //Method 1, use the following code, open online//Response.Redirect (Pdfpath); //Method 2, use the following code to let the customer downloadFileStream fs =NewFileStream (Server.MapPath (Pdfpath), FileMode.Open); byte[] File =New byte[FS. Length]; Fs. Read (file,0, file. Length); Fs. Close (); Response.Clear (); Response.AddHeader ("content-disposition","attachment; Filename="+ filename +". pdf");//forced download in binary streaming modeResponse.ContentType ="Application/octet-stream"; Response.BinaryWrite (file);

In the above code, the URL is the page to be passed, the PDF parameter is wkhtmltopdf.exe for the path you actually installed.

Of course, in the real world, if you use IIS and want to generate PDFs via ASP, you need to be aware of the permissions, first, find the application pool used by the application, click Advanced on the application pool, have a "logo", Modify the default applicationpoolidentity to LocalSystem. Otherwise, it may fail to invoke EXE because of insufficient permissions.

ASP. NET C # export PDF based on HTML page

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.