Generate Pdf Documents from a html page sale available

xiaoxiao2021-03-06  41

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

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 pdf14 -f" sfilename ".pdf" sfilename ".html";

PPROCESS.STARTINFO.WORKINGDIRECTORY = M_SDRIVE ":" m_directory;

PPROCESS.START ();

Return (sfilename ".pdf");

}

The class PDFGenerator 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".

Streetwriter Will Save The Page Into A HTML File on The Hard Disk.The File Displaypdf.aspx and displaypdf.aspx.cs will do just what, Displays the generated pdf file when ready.

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, previously 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.

转载请注明原文地址:https://www.9cbs.com/read-53710.html

New Post(0)