#native_company# #native_desc#
#native_cta#

Generating Printer Friendly Pages

By Mark Spink
on December 14, 2000

A lot of news and information sites have introduced so called Printer Friendly Pages that provide an alternative layout for their pages that will reproduce better when printed out. However, there doesn’t seem to be many sites that explain about how this achieved, let alone supply any snippets of source code. It turns out that with PHP producing printer friendly versions of your pages is not all that difficult.

So what are we trying to achieve with printer friendly versions of our web pages? Well it depends on your site and the layout you use as to how much processing of the pages is required, but in general the type of changes required are:

Page width – The width of the page needs to be restricted, to print on A4 or letter size paper about 630 pixels wide.

Background color – In general black text on a white background prints best.

Remove Adverts – Suppress adverts on this page.

Table background colors – Often information and headers in tables are emphasized using colours, these need to be removed.

Links – Hyperlinks in the page are also changed so the URL is visible, suggested by colleagues at GBDirect http://www.gbdirect.co.uk for example the link to GBDirect could be presented as:

GBDirect (http://www.gbdirect.co.uk/).

Menus – Menus are the most difficult item to suppress, however if your pages are constructed using templating this can be got around by simply using a different template for printer friendly pages with a plain layout without menus.

The overall scheme to produce a printer friendly page is quite simple, in the pages where a printer friendly option is required, place some script like this in your page


<?
// extract the required filename and relative path
// from the environment
$page=substr($SCRIPT_NAME,1);

// display an icon and link to the printer friendly
// page pfp.php3
?>
<a href="pfp.php3?page=<?=$page?>">;
  <img src="printer.gif" width="36" height="36" border="0"
   alt="Click here to produce a printer friendly page">
<font face="arial, helvetica" size="2"> Printer Friendly Version </font> </a>

This passes the name of the current page to the script ‘pfp.php3’ which loads the page into a string, using the PHP function ‘file’. Once the page has been loaded, its then a matter of writing a script to process the string and to either add, rewrite or remove sections of the html. The type and amount of processing that you will require on your page will depend on what you have included in your html. The example script below is what I commonly use,



<?
 ereg('^.*/',$SCRIPT_FILENAME,$tmp);
 $page_path = substr($tmp[0],0,-1);
?>

<html>

<head>
  <base href="http://<? echo $HTTP_HOST ?>/">
  <meta name="robots" content="no index, no follow">
  <title>Printer Friendly Page</title>
</head>

<body bgcolor="white">

<font face="Arial,Helvetica">

<table border="0" cellpadding="5" cellspacing="0" width="630" >

  <tr>
    <td valign="top">
      <?
        // check if the filename for the page exists
        if (!file_exists("$page.inc"))
        {
           echo "<strong>Error - The page <?=$page?>".
                "does not exist on this site.</strong>";
        }
        else
        {
          // get the page content and place in a string
          $fcontents = join('', file("$page.inc"));

          // convert color attribute to ignore and bgcolor to bgignore
          // this silently disables all color
          $fcontents = ereg_replace('color','ignore',$fcontents);

          // remove the target to a blank window attribute in links
          $fcontents = ereg_replace('target="_blank"','',$fcontents);

          // replace the link end element with a single character marker
          $fcontents = ereg_replace('</a>','',$fcontents);

          // show links to absolute url references
          $fcontents = ereg_replace('<a[^h]*href="(http://[^"]*)"[^>]*>([^]*)',
          '<strong>2</strong><em>(1)</em>',$fcontents);

          // show relative links with full url to home site
          $fcontents = ereg_replace(
              '<a[^h]*href="([^"]*)"[^>]*>([^]*)',
       "<strong>2</strong><em>(http://$HTTP_HOST/1)</em>",
             $fcontents);

          // since my preferred background color is white I need only
          // can change the body color tag back
          $fcontents = ereg_replace('<body bgignore','<body bgcolor',  $fcontents);

         // if any markers left restore link end element
         $fcontents = ereg_replace('','</a>',$fcontents);

         // finally print the page
         echo $fcontents;
       }
      ?>
    </td>
  </tr>

  <tr>
    <td align="center"><hr width="90%"></td>
  </tr>
 
  <tr>
    <td align="center">
      <? include("$page_path/footer.inc"); ?>
    </td>
  </tr>
 
</table>
 
</font>
 
</body>
</html>                                                                         

I hope that you have found this article of use, and I’m always interested in hearing anyones comments, particularly if you know a better way to achieve the same goal, please share it.

Freely free to visit my website named ARIA http://www.aria.uklinux.net and where I work http://www.key-electronics.co.uk to see the printer friendly code in action.

Mark Spink
[email protected]