Click here to Skip to main content
11,414,234 members (71,452 online)
Click here to Skip to main content

Generate PDF documents from a HTML page using ASP.NET

, 23 May 2004
Rate this:
Please Sign up or sign in to vote.
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 [modified] Pin
Abramson uri, 1-Mar-13 15:21
memberAbramson uri1-Mar-13 15:21 
AnswerRe: have you tried [link removed] / ? Pin
Jibesh, 1-Mar-13 15:42
memberJibesh1-Mar-13 15:42 
QuestionCould not load type 'SuperJockey.PDF.DisplayPDF'. Pin
MCA.Bhupendra, 10-Feb-13 0:29
memberMCA.Bhupendra10-Feb-13 0:29 
AnswerRe: Could not load type 'SuperJockey.PDF.DisplayPDF'. Pin
Cum7me, 30-Jul-14 3:30
memberCum7me30-Jul-14 3:30 
Questionhtml to pdf Pin
sweval, 28-Dec-11 23:09
membersweval28-Dec-11 23:09 
GeneralEO HTML to PDF converter (C# and VB.NET) Pin
szchenh, 14-Apr-11 12:17
memberszchenh14-Apr-11 12:17 
GeneralReg DLL file Pin
vimaldreams10, 6-Apr-11 5:03
membervimaldreams106-Apr-11 5:03 
GeneralHTML to PDF converter based on Webkit [ .NET / JAVA / C++ / PHP / Python / Ruby ] Pin
codeivan, 7-Feb-11 13:08
membercodeivan7-Feb-11 13:08 
Generalbest way to convert HTML to pdf from your with all styling and formatting Pin
go2amitech, 30-Jul-10 21:58
membergo2amitech30-Jul-10 21:58 
GeneralMy vote of 1 Pin
Avinash T P, 14-Jul-10 20:52
memberAvinash T P14-Jul-10 20:52 
GeneralHTML to PDF [modified] Pin
Florin Chivu, 7-Mar-10 7:36
memberFlorin Chivu7-Mar-10 7:36 
GeneralMy vote of 2 Pin
AJFK, 2-Feb-10 5:49
memberAJFK2-Feb-10 5:49 
GeneralI got error: C:\Windows\system32\LIBEAY32.dll is not a valid windows image Pin
halifaxdal, 3-Apr-09 11:41
memberhalifaxdal3-Apr-09 11:41 
GeneralNot able to view DisplayPDF.aspx page. Pin
riteshmanglani, 8-Jan-09 21:16
memberriteshmanglani8-Jan-09 21:16 
GeneralOpen source / free HTMLDOC or GHTMLDOC download site Pin
daluu, 21-Nov-08 12:59
memberdaluu21-Nov-08 12:59 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff, 13-Apr-09 18:35
memberpc.huff13-Apr-09 18:35 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
daluu, 14-Apr-09 9:38
memberdaluu14-Apr-09 9:38 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff, 14-Apr-09 9:42
memberpc.huff14-Apr-09 9:42 
GeneralRe: Open source / free HTMLDOC or GHTMLDOC download site Pin
pc.huff, 14-Apr-09 9:45
memberpc.huff14-Apr-09 9:45 
GeneralDownloading PDF Pin
Vinoth Dhakshinamoorthy, 17-Jun-08 4:16
memberVinoth Dhakshinamoorthy17-Jun-08 4:16 
GeneralRe: Downloading PDF Pin
bkstylz, 13-Aug-08 9:30
memberbkstylz13-Aug-08 9:30 
GeneralRe: Downloading PDF Pin
DarthJay, 29-Aug-08 11:47
memberDarthJay29-Aug-08 11:47 
GeneralRe: Downloading PDF Pin
eva xanthou, 20-May-09 6:15
membereva xanthou20-May-09 6:15 
GeneralRe: Downloading PDF Pin
jlfritz, 16-Dec-10 5:07
memberjlfritz16-Dec-10 5:07 
GeneralRe: Downloading PDF Pin
Gudny, 26-Sep-11 4:06
memberGudny26-Sep-11 4:06 
QuestionPermissions? Pin
jd_hannah, 6-Jun-08 11:51
memberjd_hannah6-Jun-08 11:51 
AnswerRe: Permissions? Pin
jd_hannah, 6-Jun-08 12:29
memberjd_hannah6-Jun-08 12:29 
GeneralCode / Bug Fix Pin
Justin Porteous, 1-Jun-08 23:17
memberJustin Porteous1-Jun-08 23:17 
GeneralRe: Code / Bug Fix Pin
Md. Humayun Rashed, 29-Jul-12 20:19
memberMd. Humayun Rashed29-Jul-12 20:19 
Generalghtmldoc.exe OR HTMLDOC.exe for HTMLDOC 1.9 Pin
deepakleo, 20-Dec-07 20:20
memberdeepakleo20-Dec-07 20:20 
Generalghtmldoc.exe for HTMLDOC 1.9 Pin
deepakleo, 20-Dec-07 20:18
memberdeepakleo20-Dec-07 20:18 
GeneralProblem with visual studio 2005 Pin
Mamta M, 26-Jul-07 2:04
memberMamta M26-Jul-07 2:04 
I am using this tool to convert HTML file to PDF file. The code works fine with .Net 1.1 version however it does not work with .Net 2.0.
The PDF file is not getting generated. Is this code is only compatible with .Net 1.1?


General100% Managed HTML to PDF Converter Library Pin
winnovative, 8-Jul-07 11:03
memberwinnovative8-Jul-07 11:03 
GeneralPretty useless Pin
dc197, 19-Apr-07 4:52
memberdc19719-Apr-07 4:52 
GeneralRe: Pretty useless Pin
Alexander German, 5-Jul-07 1:35
memberAlexander German5-Jul-07 1:35 
GeneralRe: Pretty useless Pin
DeCrypted13, 7-Aug-07 9:07
memberDeCrypted137-Aug-07 9:07 
GeneralRe: Pretty useless Pin
Robert Jun, 25-May-14 22:58
memberRobert Jun25-May-14 22:58 
GeneralI can not convet from HTML to PDF Pin
minhhong0411, 2-Apr-07 1:02
memberminhhong04112-Apr-07 1:02 
GeneralRe: I can not convet from HTML to PDF Pin
deepakleo, 20-Dec-07 19:27
memberdeepakleo20-Dec-07 19:27 
GeneralRe: I can not convet from HTML to PDF Pin
deepakleo, 20-Dec-07 19:28
memberdeepakleo20-Dec-07 19:28 
GeneralFailed to map the path '/'. Pin
amper_1, 20-Mar-07 0:02
memberamper_120-Mar-07 0:02 
GeneralRe: Failed to map the path '/'. Pin
deepakleo, 20-Dec-07 19:31
memberdeepakleo20-Dec-07 19:31 
GeneralCharset Pin
Fernando Callejon, 3-Jan-07 12:46
memberFernando Callejon3-Jan-07 12:46 
QuestionNew .net developer [modified] Pin
nauyshine, 13-Nov-06 17:07
membernauyshine13-Nov-06 17:07 
Questiongenerated pdf from html contains "default" in header Pin
jhlal, 8-Nov-06 3:07
memberjhlal8-Nov-06 3:07 
QuestionASP .Net 2.0 Pin
rav79, 5-Oct-06 13:02
memberrav795-Oct-06 13:02 
QuestionPDF to HTML?? Pin
xvietntx, 20-Sep-06 15:59
memberxvietntx20-Sep-06 15:59 
GeneralError plz help Pin
Sankar 123, 4-Aug-06 9:03
memberSankar 1234-Aug-06 9:03 
GeneralRe: Error plz help Pin
tiodio, 16-Mar-07 2:55
membertiodio16-Mar-07 2:55 
Generalghtmldoc is notworking in 2.0 ???? [modified] Pin
Hemant Shrivastava, 20-Jul-06 22:01
memberHemant Shrivastava20-Jul-06 22:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.2 | Last Updated 24 May 2004
Article Copyright 2004 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid