Generate PDF documents from a HTML page using ASP. NET

Source: Internet
Author: User
Tags class generator

Introduction

This project uses an HTML to pdf exe from ESP. please read the GNU license agreement for more information. htmldoc is a desktop application to create PDF documents from a HTML page. I wrote some code to use it from a Web application. the best used is from a Web Report to add a print to PDF button to use the C # class.

Using the code

 

Code
Public   String Run ( String Srawurl)
{
String Sfilename = Getnewname ();
String Spage = Server. mappath ( ""   + Sfilename +   " . Html " );
String Surlvirtual = Srawurl;
Stringwriter SW =   New Stringwriter ();

Server. Execute (surlvirtual, SW );

Streamwriter swriter=File. createtext (spage );
Swriter. writeline (SW. tostring ());
Swriter. Close ();

System. Diagnostics. Process pprocess
=   New System. Diagnostics. Process ();
Pprocess. startinfo. filename = M_sdrive +   " : "   + M_directory +  
" \ Ghtmldoc.exe " ;
Pprocess. startinfo. Arguments =   " -- Webpage -- quiet "   + Sfontsize +  
M_swatermark +   " -- Bodyfont Arial "   + Slandscape +  
" -T limit 14-F "   + Sfilename +   " . PDF "   + Sfilename +   " . Html " ;
Pprocess. startinfo. workingdirectory = M_sdrive +   " : "   + M_directory;

Pprocess. Start ();

Return(Sfilename+ ". PDF");
}

 

The class generator contains a public method called run that will call the process hghtmldoc.exe with the arguments you choose. the most important part is to set a working directory where the web application has permission to read, write and execute, otherwise the program won't work, and the function pprocess. start will raise a Win32 exception "Access Denied ".

Streamwriter will save the page into a HTML file on the hard disk.

The file displaypdf. aspx and displaypdf. aspx. CS will do just that, displays the generated PDF file when ready.

 

Code
Private   Void Page_load ( Object Sender, system. eventargs E)
{

If (Request. Params [ " File " ] ! =   Null )
{
Bool Bret =   False ;
Int Itimeout =   0 ;
While (Bret =   False )
{
Bret = Checkiffileexist (request. Params [ " File " ]. Tostring ());
Thread. Sleep ( 1000 );
Itimeout ++ ;
If (Itimeout =   10 )
Break ;
}

If (Bret =   True )
{
Response. clearcontent ();
Response. clearheaders ();
Response. contenttype =   " Application/PDF " ;
Try  
{
Response. writefile (mappath ( ""   +  
Request. Params [ " File " ]. Tostring ()));
Response. Flush ();
Response. Close ();
}
Catch  
{
Response. clearcontent ();
}

}
Else
{
If (Request. Params [ " MSG " ] ! =   Null )
{
Labelmsg. Text = Request. Params [ " MSG " ]. Tostring ();
}
}
}
}

 

The page accepts a parameter, file, previusly saved in the hard disk by streamwriter. the response. redirect will include application/pdf, so the browser knows what kind of file is downloading and ask you to save or open. if you have adobe plug-in installed on your browser, you'll be able to see the PDF from your browser.

Here is the original link,

Http://www.codeproject.com/KB/aspnet/HTML2PDF.aspx

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.