Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
 
Add your own
alternative version
Go to top

Generate PDF documents from a HTML page using ASP.NET

, 23 May 2004
Convert HTML to PDF from an ASPX page.
HtmlToPDF.zip
HtmlToPDF
HtmlToPDF.csproj.webinfo
bin
HtmlToPDF.dll
HtmlToPDF.pdb
Global.asax
data
cp-1250
cp-1251
cp-1252
cp-1253
cp-1254
cp-1255
cp-1256
cp-1257
cp-1258
cp-874
iso-8859-1
iso-8859-14
iso-8859-15
iso-8859-2
iso-8859-3
iso-8859-4
iso-8859-5
iso-8859-6
iso-8859-7
iso-8859-8
iso-8859-9
koi8-r
prolog.ps
psglyphs
doc
htmldoc.pdf
fonts
Courier-Bold.afm
Courier-Bold.pfa
Courier-BoldOblique.afm
Courier-BoldOblique.pfa
Courier-Oblique.afm
Courier-Oblique.pfa
Courier.afm
Courier.pfa
Helvetica-Bold.afm
Helvetica-Bold.pfa
Helvetica-BoldOblique.afm
Helvetica-BoldOblique.pfa
Helvetica-Oblique.afm
Helvetica-Oblique.pfa
Helvetica.afm
Helvetica.pfa
Symbol.afm
Symbol.pfa
Times-Bold.afm
Times-Bold.pfa
Times-BoldItalic.afm
Times-BoldItalic.pfa
Times-Italic.afm
Times-Italic.pfa
Times-Roman.afm
Times-Roman.pfa
ghtmldoc.exe
libeay32.dll
ssleay32.dll
-t
Uninst.isu
vssver.scc
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Threading;

namespace HtmlToPDF.PDF
{
	/// <summary>
	/// Summary description for DisplayPDF.
	/// </summary>
	public class DisplayPDF : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label LabelMsg;
		protected System.Web.UI.WebControls.HyperLink HL_Adobe;
		protected System.Web.UI.WebControls.Button BT_PDFReady;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.Params["Page"] != null )
			{
				PDF.PDFGenerator pGen = new PDF.PDFGenerator(Server.MapPath("/"));
				string sFile = pGen.RunWeb(Request.Params["Url"].ToString());

				Response.Redirect("DisplayPDF.aspx?File=" + sFile);
			}

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

		private bool CheckIfFileExist(string sFile)
		{
			string sRoot = Server.MapPath("/");
			string sDrive = "" + sRoot[0];

			if ( File.Exists(MapPath( "" + Request.Params["File"].ToString() )) == false )
			{			
				
				return(false);
			}

			return(true);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.BT_PDFReady.Click += new System.EventHandler(this.BT_PDFReady_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void BT_PDFReady_Click(object sender, System.EventArgs e)
		{
			string sMsg = "Please wait a few seconds and press the button again";

			if( Request.Params["Msg"] != null )
				sMsg = "This report takes longer than normal, wait 10 seconds and press the button again";

			if ( Request.Params["File"] != null )
			{
				Response.Redirect("DisplayPDF.aspx?File=" + Request.Params["File"].ToString() + "&Msg=" + sMsg);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

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
 
Blog

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 May 2004
Article Copyright 2004 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid