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

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.IO;

namespace HtmlToPDF.PDF
{
	/// <summary>
	/// Summary description for PDFGenerator.
	/// </summary>
	public class PDFGenerator : System.Web.UI.Page
	{
		private string sLandScape;
		private string m_sWaterMark;
		private string m_sDrive;
		private string m_Directory;
		public PDFGenerator(string sDirectory)
		{
			sFontSize = "";
			sLandScape = "--landscape";
			m_sWaterMark = "";
			string sRoot = Server.MapPath("/");
			m_sDrive = "" + sRoot[0];
			m_Directory = sDirectory;
		}

		private string sFontSize;
		public string FontSize
		{
			set { sFontSize = "--fontsize " + value; }
			get { return(sFontSize);}
		}

		public void SetLandScape(bool bRet)
		{
			if ( bRet == false )
				sLandScape = "";
		}

		public void SetWaterMark (bool bRet)
		{
			if ( bRet == false)
				m_sWaterMark = "";

		}

		public string RunWeb(string sUrl)
		{
			string sFileName = GetNewName();

			System.Diagnostics.Process	pProcess = new System.Diagnostics.Process();
			pProcess.StartInfo.FileName = m_sDrive + ":" + m_Directory + "\\ghtmldoc.exe";
			pProcess.StartInfo.Arguments = "--webpage --quiet " + sFontSize + " --bodyfont Arial " + sLandScape + " -t pdf14 -f " + sFileName + ".pdf " + sUrl;
			pProcess.StartInfo.WorkingDirectory = m_sDrive + ":" + m_Directory;
			
			pProcess.Start();
						
			return(sFileName + ".pdf");			
		}

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

		private string GetNewName()
		{
			string sName = Convert.ToString(DateTime.Now.Ticks);
			return(sName);
		}
	}
}

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

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
Web04 | 2.8.140721.1 | Last Updated 24 May 2004
Article Copyright 2004 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid