Click to See Complete Forum and Search --> : [RESOLVED] PHP Printer-Friendly Page


swagner
10-14-2007, 10:45 AM
Hello,

I recently found a great code to create a print.php page that dynamically extracts all the contents of the page that is specified in the url:
For example:
http://www.domain.com/print.php?page=index.htm

Of course, the 'page=' variable would change from page to page...

Here is the code I found:
<?
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"))
{
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"));

// 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>

</table>

</font>

</body>
</html>
(Source: http://www.aria.uklinux.net/pfp.php)
Now, I tried this on my server, but every time, it gives me the error message (see code above). I do not know why. Any help would be great.

Thanks,
SWagner

JoeHoldcroft
10-14-2007, 12:04 PM
Where is the error message? Looks like it could be your configuration in the php.ini file, as the file uses lots of things that are sometimes turned off on some servers such as short tags and registering globals.
But for us to know exactly where the problem is, we need to know that the error message says! Do you mean the error in the code saying the page could not be found is showing? If so, looks like your host has register_globals set to off. Change this line:

if (!file_exists("$page")) to this:
if (!file_exists($_GET['page']))

swagner
10-14-2007, 03:16 PM
The error message was the one defined inside the code. I changed what you told me to, but now there is a new error:
Warning: join() [function.join]: Bad arguments. in /opt/www/courses2-port80/wagnerlab/print.php on line 24
Line 24 contains this code:
$fcontents = join('', file("$page"));
This is the code that takes the page contents and puts them into a string, about which I actually have a question. Would it be possible to just take the body and put it in a string, instead of the entire page?

JoeHoldcroft
10-14-2007, 04:29 PM
Same problem there, change line 24 to:
$fcontents = join('', file($_GET['page']));
Basically, change any instance of $page to $_GET['page'], or put this line at the top of the code:
ini_set('register_globals', 'true');

..would probably be an easier way of doing it :).

That would be possible, you'd need to split the string at '<body>' and '</body>' and just use the middle. I could post some code to do this once the error is fixed.

swagner
10-14-2007, 05:04 PM
Okay, the error is fixed. Go ahead and tell me the code...

JoeHoldcroft
10-14-2007, 05:30 PM
Try this:

$fcontents = join('', file($_GET['page']));

$split = explode('<body>', $fcontents);

$split2 = explode('</body>', $split[1]);

$fcontents = $split2[0];

swagner
10-14-2007, 05:33 PM
Thanks, but the page is blank...

JoeHoldcroft
10-14-2007, 05:42 PM
Apologies, I edited my previous post with the new code. Make sure you insert it starting at line 24.

swagner
10-14-2007, 05:45 PM
Sorry, blank page again...
(code is on line 24!)

JoeHoldcroft
10-14-2007, 05:49 PM
Try now (sorry about this, late for me on a Sunday!)

swagner
10-14-2007, 06:07 PM
Sorry again... (This is crazy!!:eek: ). I don't know what is wrong...

felgall
10-14-2007, 09:08 PM
Is there some reason why you don't just attach a print stylesheet to your pages to define how they should print. A separate printer friendly page was the old way to do it.

swagner
10-15-2007, 06:41 AM
I have heard of doing it like that. But wouldn't it require all of my pages to have a .php extension? I do not want to rename all of my pages from .htm to .php...