Many methods exist to creating printer versions. A few of these include:
The method I will describe here is different from the usual methods and is used on websites like CNN.
The basic idea is to put a hidden html comment tag at the beginning of the main content of your webpage, and another hidden html comment tag at the end of your content.
The content you want to print goes here.
The Printer Version script will open the webpage, grab only that which is between the hidden tags, and display it within the Printer Version template.
Bells & Whistles
A few extra features are included to provide additional functionality:
Set up MySQL database tables
The first table to create will hold member settings. If you create an interface for members to manage their own data, then username and password will be useful. Name and email may be useful for you to contact your users or notify them with custom email messages via an error subroutine. The "activeyn" field should be set to "y" if you want the user’s printer version to work. You may specify a different template file such as "customername.html" in the "template" field. The "subject", "logo", and "emailbody" fields field are added in anticipation of having an "Email This Page To A Friend" script to interact with this same data in the future. The "domain" field is the allowed domain name for this user. The "sitename" field should be the name of the user’s company and will be available for integration with templates.
CREATE TABLE `member` (
  `id` tinyint(7) NOT NULL auto_increment,
  `username` varchar(25) NOT NULL default '',
  `password` varchar(25) default NULL,
  `name` varchar(50) default NULL,
  `email` varchar(50) default NULL,
  `activeyn` char(1) default NULL,
  `template` varchar(50) default NULL,
  `subject` varchar(100) default NULL,
  `domain` varchar(50) default NULL,
  `logo` varchar(100) default NULL,
  `emailbody` mediumtext,
  `sitename` varchar(100) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
Here is an example SQL statement used to set up a user:
INSERT INTO `member` VALUES (1, '', 'yourpassword', 
'Your Company Name', '', 'y', 
'customtemplate.html', 'Website Page From !MAILFROMNAME!', 
'', 'yourlogo.jpg', 
'The following page from the Your Company Name 
website\r\n( was suggested to you by: 
!MAILFROMNAME!.\r\nPage Title: !TITLE!\r\nLink: 
!URL!\r\nNote from !MAILFROMNAME!:\r\n!MESSAGE!', 
'Your Company Name');
Next we create the cache table. It is relational to the member table via the username field. Each record will store the URL of the respective webpage in all lowercase, the timestamp of when it was added to the table for use in expiration, the <head> title of the webpage, and the printable content.
CREATE TABLE `cache` (
  `id` tinyint(7) NOT NULL auto_increment,
  `username` varchar(25) NOT NULL default '',
  `url` varchar(100) default NULL,
  `date` varchar(20) default NULL,
  `title` varchar(50) default NULL,
  `body` blob,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
Create the template
The default template name for the printable version will be 1.html . You may define a different default template for a domain in the MySQL settings for that member.
The most basic template will use 3 tokens: Page Title is !TITLE!, URL to original page is !URL!, and the printable content is !CONTENT! A more fully-featured template which includes both "Print" and "Close This Window" buttons is included in the download version of this script.
<base href="!URL!"></head>
<br><br>Page printed from: !URL!
Beginning the Printer Version script
Create a new file called print.php and start the file with:
A few options need to be set in the beginning.
First are the MySQL server connections.

//Server Settings
$mysv="localhost"#your server name
$myun="your MySQL username";
$mypw="your MySQL password";
$mydb="your MySQL database name";
$db mysql_connect($mysv$myun$mypw);
Next we tell the script how many days to cache the printable HTML content of a webpage. After this many days the script will go spider the webpage again. If your webpages change frequently, you may wish to make this a lower number.

Next we define what hidden comment tags we will use to grab the printable content.

$GrabEnd "<!--endclickprintinclude-->";
You have the option of excluding certain content that is inside of the printable region. Define the comment tags here.

$ExcludeEnd "<!--endclickprintexclude-->"
This defines what tags we will use to find the webpage title.

$TitleEnd "</title>"
Determine the original webpage
Depending on your implementation, you may pass a variable to the script telling it what URL to spider. If the script does not receive this variable, it will try to determine the URL using the $HTTP_REFERER variable.

//Get The URL to spider
if (!$url) {
Verify user
This script is designed for you to set up multiple domain names which are allowed to use the Printer Version script. No other domain names will be allowed to use the script.
To accomplish these we will first extract the domain name from the URL:

$url2 str_replace("http://"""$url2);
$url2 str_replace("www."""$url2);
$path explode ("/"$url2);
$domainname $path[0];
Next we will query the MySQL database to look for a match for any users which match the domainname just parsed. If the user is active, then the settings for that user will be available later in the script in an array called $myrow.

"SELECT * FROM member WHERE domain='$domainname'";
$result mysql_query($sql);
$myrow mysql_fetch_array($result);
Even if a user exists, does not mean that they are active. If the user does not exist or is not active, we will return an error message.

if ($myrow["activeyn"]!="y"){
"This domain $domainname is not active";
Retrieve the template
You can specify different template filenames for different users in their database record. The default template is 1.html . Templates should be stored in the same directory as the print.php script. Once opened, the template code is stored in the variable $body.

//Get template filename for this user from database
$filename $myrow["template"];
//Show the default template if necessary    
if ( !file_exists($filename) ) {
$filename "1.html";
//Open template file and store in variable $body

$fd fopen ($filename, &quot;rb&quot;);
$body fread ($fdfilesize ($filename));
fclose ($fd);
Search the cache for page content
Here we will build a function to see if this page is already in the MySQL cache of page content. If it is in the cache, the function will only return the data if it is not older than the number of days specified in the options above.
Step 0: Define the function

function cachesearch($url,$daystocache)
Step 1: Calculate the maximum timestamp available

strtotime (&quot;-$daystocache days&quot;);
Step 2: Query database for matching URL and timestamp below the maximum allowed

"SELECT * FROM cache WHERE url='$url' and date > '$expiredate'";
$result mysql_query($sql);
$myrow mysql_fetch_array($result);
Step 3: If matching content is in the cache, return it

if (mysql_numrows($result)==1) {
$answer[0] = $myrow["title"];
$answer[1] = $myrow["body"];
Step 4: If no match is found in the cache, return false

else {
Step 5: End the function definition

# end of cachesearch
Step 6: Execute the function

$resultb cachesearch($cacheurl,$daystocache);
Note: Any returned title will now be in variable $result[0] and any returned body content will now be in variable $result[1] .
Retrieve webpage content if not in cache
Here we will check to see if we successfully retrieved any content from the cache. If the cache was empty or expired for this URL, we will go grab the webpage.

if ($resultb[1]==’’) {
First we grab the entire webpage and store in variable $rf.

$rf fread($file20000);
Next we parse out the printable content using the tags defined in the options above.

Next we delete any content chosen to be excluded. The printable content is now ready and stored in variable $showme.

Next we parse out the page title, but only if a title has not been sent to the script via a variable.

if (!$title) {
$grab eregi("$TitleStart(.*)$TitleEnd"$rf$thetitle);
$title $thetitle[1];
#end if no title
Add grabbed data to MySQL cache. First delete any old cached data.

"DELETE FROM cache where url='$cacheurl'";
$resulta mysql_query($sql);
Next Create the new data’s timestamp.

Next format the new data for the database.

$contentb mysql_escape_string($showme);
Finally insert the new data into the database.

"INSERT INTO cache (id,username,url,date,title,body) VALUES 
$resultc mysql_query($sql);
Finish up by closing the if expression.

#end if cache is empty
If the page is in the cache, then set the $showme and $title variables based on the returned data.

else {

$showme $resultb[1];
$title $resultb[0];

Integrate printable content with template
The template content is in the $body variable. We will replace the tokens in the template with the variables in this script.

$body2 str_replace("!URL!"$url$body2);
$body2 str_replace("!COMPANY!"$company$body2);
$body2 str_replace("!HTTP_REFERER!"$HTTP_REFERER$body2);
$body2 str_replace("!CONTENT!"$showme$body2);
$body2 str_replace("!LOGO!"$myrow["logo"], $body2);
$body2 str_replace("!SITENAME!"$myrow["sitename"], $body2);
Show the result
Print the resulting printer version of the webpage and end the print.php script.

echo $body;

Adding Printer Version button via HTML
This method involves hard-coding the link to the printer version. You can hard code ALL of the data this way:
<a href=” 
<img src=”” border=0></a>
Alternatively, you can let the print.php script determine the URL of the page to be grabbed using $HTTP_REFERER. In this case, your may use the same link throughout your website.
<a href=””>
<img src=”” border=0></a>
Adding Printer Version button via Javascript
This method is an alternative to the HTML method, and allows you to control the HTML for the Printer Version button and link in a central javascript file.
1. Add the following javascript to your webpage template in the place that you want the Printer Version button to appear:
<script language="javascript" 
<script language=javascript>showbutton 
The script allows you to specify a particular "Printer Version" button. The default button is "printmain.gif" but you can override this for a particular webpage by changing the word "main" to some other word. (e.g. if you want to use "printblack.gif" button instead, change the word "main" to "black")
2. Create a file called print.js.
The showbutton() function creates the HTML to show the printer version button and link to the printer version.
The showprinterversion() function opens a new window to show the printer version. You can customize the size and features of the window here. This function automatically URL encodes the URL of the page to be grabbed, and uses javascript to complete the title of the page.
The randomname() function creates a random name for the opened window. This helps prevent some cache issues.
function randomname(str)
	var ret = new String("");
 	var ch;
 	for (var i = 0; i < str.length; i++)
 	ch = str.charAt(i);
	if (ch == ".")
		ret += "X";
		ret += ch;
	 return ret;

function showprinterversion(company, button)
var name = randomname(Math.random().toString());
var url = "" 
+ company + "&url=" +escape(location.href) + "&title=" 
+ escape(document.title);, name, 

function showbutton(company, button)
document.write('<table border="0" cellspacing="0" 
document.write('<tr><td align="middle" valign="top">');
var link = "<a name='printerversionlink' href='#' 
onClick='javascript:showprinterversion(\"" + company 
+ "\", \"" + button + "\");
' target='_self'>";
document.write (link);
var btn = "<img name='printerversionbutton' 
src='" + button 
+ ".gif' border=0>";
document.write (btn);
Testing it out
In the end you should have the following files:
I also include some images to use in the template:
the sample SQL needed to set up your database:
and an example page that shows how to use the include and exclude tags:
Good luck!
Jim Fletcher –