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