Click here to Skip to main content
15,393,377 members
Articles / Web Development / ASP.NET
Posted 17 May 2004


202 bookmarked

Generate PDF documents from a HTML page using ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.34/5 (62 votes)
23 May 20041 min read
Convert HTML to PDF from an ASPX page.


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);

    System.Diagnostics.Process pProcess
                         = new System.Diagnostics.Process();
    pProcess.StartInfo.FileName = m_sDrive + ":" + m_Directory +
    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;


    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".

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.

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());
            if ( iTimeout == 10 )

        if ( bRet == true )
            Response.ContentType = "Application/pdf";
                Response.WriteFile( MapPath( "" +
                              Request.Params["File"].ToString() ) );

            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.

Points of Interest

It is important you create a directory to save the HTML file and generate the PDF, also give that directory all the permissions you need to run the EXE.


Remember the HTMLDOC is copywrite ESP. Please go to this link and download the latest version. The GNU license agreement is included in the project.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Albert Pascual
Web Developer
United States United States
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP


Comments and Discussions

QuestionmagicPDF should do the trick Pin
Abramson uri1-Mar-13 14:21
MemberAbramson uri1-Mar-13 14:21 
AnswerRe: have you tried [link removed] / ? Pin
Jibesh1-Mar-13 14:42
professionalJibesh1-Mar-13 14:42 
QuestionCould not load type 'SuperJockey.PDF.DisplayPDF'. Pin
Er_Bhupendra9-Feb-13 23:29
MemberEr_Bhupendra9-Feb-13 23:29 
AnswerRe: Could not load type 'SuperJockey.PDF.DisplayPDF'. Pin
Cum7me30-Jul-14 2:30
MemberCum7me30-Jul-14 2:30 
QuestionMessage Closed Pin
28-Dec-11 22:09
Membersweval28-Dec-11 22:09 
GeneralMessage Closed Pin
14-Apr-11 11:17
Memberszlovedog14-Apr-11 11:17 
GeneralReg DLL file Pin
vimaldreams6-Apr-11 4:03
Membervimaldreams6-Apr-11 4:03 
GeneralMessage Closed Pin
30-Jul-10 20:58
Membergo2amitech30-Jul-10 20:58 
GeneralMessage Closed Pin
7-Mar-10 6:36
MemberFlorin Chivu7-Mar-10 6:36 
GeneralMy vote of 2 Pin
AJFK2-Feb-10 4:49
MemberAJFK2-Feb-10 4:49 
GeneralI got error: C:\Windows\system32\LIBEAY32.dll is not a valid windows image Pin
halifaxdal3-Apr-09 10:41
Memberhalifaxdal3-Apr-09 10:41 
GeneralNot able to view DisplayPDF.aspx page. Pin
riteshmanglani8-Jan-09 20:16
Memberriteshmanglani8-Jan-09 20:16 
GeneralOpen source / free HTMLDOC or GHTMLDOC download site Pin
daluu21-Nov-08 11:59
Memberdaluu21-Nov-08 11:59 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff13-Apr-09 17:35
Memberpc.huff13-Apr-09 17:35 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
daluu14-Apr-09 8:38
Memberdaluu14-Apr-09 8:38 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff14-Apr-09 8:42
Memberpc.huff14-Apr-09 8:42 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff14-Apr-09 8:45
Memberpc.huff14-Apr-09 8:45 
GeneralDownloading PDF Pin
Vinoth Dhakshinamoorthy17-Jun-08 3:16
MemberVinoth Dhakshinamoorthy17-Jun-08 3:16 
GeneralRe: Downloading PDF Pin
bkstylz13-Aug-08 8:30
Memberbkstylz13-Aug-08 8:30 
GeneralRe: Downloading PDF Pin
DarthJay29-Aug-08 10:47
MemberDarthJay29-Aug-08 10:47 
GeneralRe: Downloading PDF Pin
eva xanthou20-May-09 5:15
Membereva xanthou20-May-09 5:15 
GeneralRe: Downloading PDF Pin
jlfritz16-Dec-10 4:07
Memberjlfritz16-Dec-10 4:07 
GeneralRe: Downloading PDF Pin
Gudny26-Sep-11 3:06
MemberGudny26-Sep-11 3:06 
QuestionPermissions? Pin
jd_hannah6-Jun-08 10:51
Memberjd_hannah6-Jun-08 10:51 
AnswerRe: Permissions? Pin
jd_hannah6-Jun-08 11:29
Memberjd_hannah6-Jun-08 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.