[SOLVED] PHP Printer-Friendly Page

This is a discussion on "[SOLVED] PHP Printer-Friendly Page" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP Printer-Friendly Page are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 13th, 2007, 13:13
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP Printer-Friendly Page

[This is a follow-up from a thread in the Javascript Forum]

Hello,

I recently acquired web space on a server that supports php. Now I would like to use php to create a page that when accessed, outputs the contents of another page in a printer-friendly format. I have tried this with javascript on my old server (see thread).

The idea is a page called print.php that would have a "?page=/index.htm" parameter. The ?page= would be specified in a link on any page that I would like to be "printable". Can anyone help me out with this. As I said, I am new to php.

Thanks in advance,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

  #2 (permalink)  
Old Oct 13th, 2007, 13:19
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

This is the easiset thing you could imagine

On any page that you want to be printable place this where you style tags are

PHP: Select all



<?php

if (!empty($_GET['print']))
   echo 
'<link rel="stylesheet" type="text/css" href="print.css" />';
else
    echo 
'<link rel="stylesheet" type="text/css" href="normal.css" />';
?>
Then anytime you want to load the printer page, load exactly the same page buse the url like

http://www.example.com/index.php?print=1

This will load the right css because the script is checking if print is set in the query string.

Cheers,

PS. Welcome to PHP
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #3 (permalink)  
Old Oct 14th, 2007, 01:07
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

OK, looks good. But isn't there a way to just create a print.php page with a code similar to this:
PHP: Select all

<?
if ($_SERVER['HTTP_REFERER']){
    
$url = &$_SERVER['HTTP_REFERER'];
} else {
    
// Default page to go to if the script was not invoked
    // through an HTTP referral (i.e. they didn't follow a
    // link).
    
$url "http://www.domain.com/";
}

// These are the tags that determine where the printable
// content starts and where it should end. Remember to add
// them to your web pages. :)

$START_CONT="<!-- CONTENT STARTS -->";
$END_CONT "<!-- CONTENT ENDS -->";

$content=0;

// replaces any space in the query with +
$url preg_replace('/[[:space:]]/','+',$url);

if(
$document[strlen($document)-1]=='/'){

    
$document "http://www.domain.com/index.htm";
}
?>

<html>
<head>
<link rel="stylesheet" media="all" href="print.css">
</head>

<?
  $f_contents 
file($url);
  foreach(
$f_contents as $line){
      if(
ereg($START_CONT,$line)){
        
$content=1;
      }
      if(
ereg($END_CONT,$line)){
        
$content=0;
      }
      if(
$content==1){
        echo 
$line;
      }
  }
?>
</body>
</html>
Is this possible? I would actually prefer not having to use the comment tags to start and end the output of the contents of a page - just outputting the entire body would be much better...

Regards,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 14th, 2007 at 01:34.
  #4 (permalink)  
Old Oct 14th, 2007, 01:32
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

How is that any different to loading the same page again with a different style sheet.

If you are outputting the contents of the body tag on the given page, it will still have all of the HTML in it.

That code you have seems unnecessarily complex for such a simple task.

You could just use PHP to wite the head of the document only if they havn't asked to print the page.

PHP: Select all

<html>

<head>
<?php

if (!empty($_GET['print']))
   echo 
'';
else
{
    
// head information goes here
}
?>

</head>

<body>

etc....

</body>
</html>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #5 (permalink)  
Old Oct 14th, 2007, 01:40
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

OK. I see that this is a lot easier than I always thought it was. I think I will stick with the first code you gave me. Just one thing: wouldn't it also work if the link is just
HTML: Select all
<a href="?print">Print</a>
Or does it need to be
HTML: Select all
<a href="?print=1">Print</a>
I am not sure what the '=1' does. Could you explain that?

PS: Renaming all of my pages from .htm to .php ?!? This is going to be a long weekend...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 14th, 2007 at 01:45.
  #6 (permalink)  
Old Oct 14th, 2007, 02:01
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Printer-Friendly Page

If you change !empty to isset you should be able to use just print.
This is because, isset checks if the variable is just there. Whereas empty checks if the value of the variable is present.

What server are you running on? you can do an apache handler which will treat .htm files as php.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #7 (permalink)  
Old Oct 14th, 2007, 02:08
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

I am running on a Cornell University hosted server. Go there. My actual website is one directory further - here.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #8 (permalink)  
Old Oct 14th, 2007, 02:36
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

The =1 sets the value of $_GET['print'] to true, leaving it as print means it is empty and the check will fall through and won't write the style sheet that you need.

You could create a page with this function to change all of the filenames to .php

Please back up your files This should work in theory but I don't have a directory to rename in practice.

PHP: Select all



function renameFiles($directory) {
  if(
$curdir opendir($directory)) {
   while(
$file readdir($curdir)) {
     if(
$file != '.' && $file != '..') {
 
       if (
strpos($file'.htm'))
{
       
$dstfile $directory '/' substr_replace($file'.php', -4); 
       
$srcfile $directory '/' $file;     
 
       
rename($srcfile$dstfile);
 }
       if(
is_dir($srcfile)) {
          
renameFiles($srcfile);
       }
     }
   }
   
closedir($curdir);
  }

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #9 (permalink)  
Old Oct 14th, 2007, 02:38
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

Where would I put this code. The index.htm file or a different one? Also, would this actually change the extension to .php permanently, or does it just make the browser believe that the extension is .php? I am a little skeptic about my navigation acting up, not linking to the right pages anymore, and giving me my error page a hundred times over and a hundred times again.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 14th, 2007 at 02:43.
  #10 (permalink)  
Old Oct 14th, 2007, 02:46
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

In a file all by itself which you run in your browser.

call it whatever you like.

The contents of the whole file would be

PHP: Select all

<?php



renameFiles 
('full/path/to/directory');

function*
renameFiles($directory)*{

**if(
$curdir*=*opendir($directory))*{

***while(
$file*=*readdir($curdir))*{

*****if(
$file*!=*'.'*&&*$file*!=*'..')*{

*

*******if*(
strpos($file,*'.htm'))

{

*******
$dstfile*=*$directory*.*'/'*.*substr_replace($file,*'.php',*-4);*

*******
$srcfile*=*$directory*.*'/'*.*$file;*****

*

*******
rename($srcfile,*$dstfile);

*}

*******if(
is_dir($srcfile))*{

**********
renameFiles($srcfile);

*******}

*****}

***}

***
closedir($curdir);

**}

}*


?>
Then go to the file in your browser to run (but please back up as I can't be certain it will work and have not tested)

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #11 (permalink)  
Old Oct 14th, 2007, 02:52
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

Quote:
Originally Posted by swagner View Post
Also, would this actually change the extension to .php permanently, or does it just make the browser believe that the extension is .php? I am a little skeptic about my navigation acting up, not linking to the right pages anymore, and giving me my error page a hundred times over and a hundred times again.
.php extension :: permanent or temporary?

PS: The php code is showing asterisks again. I think that happens if you have the same code twice in a thread.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #12 (permalink)  
Old Oct 14th, 2007, 03:28
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

The change would be permanent so if you have an issue with misplaces links you might want to go with Alex's suggestion.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #13 (permalink)  
Old Oct 14th, 2007, 13:00
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Printer-Friendly Page

Quote:
Originally Posted by Rakuli View Post
PHP: Select all

<?php


renameFiles 
('full/path/to/directory');

function*
renameFiles($directory)*{

**if(
$curdir*=*opendir($directory))*{

***while(
$file*=*readdir($curdir))*{

*****if(
$file*!=*'.'*&&*$file*!=*'..')*{

*

*******if*(
strpos($file,*'.htm'))

{

*******
$dstfile*=*$directory*.*'/'*.*substr_replace($file,*'.php',*-4);*

*******
$srcfile*=*$directory*.*'/'*.*$file;*****

*

*******
rename($srcfile,*$dstfile);

*}

*******if(
is_dir($srcfile))*{

**********
renameFiles($srcfile);

*******}

*****}

***}

***
closedir($curdir);

**}

}*


?>
Whitespace replaced by asterixes? Must be a bug.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #14 (permalink)  
Old Oct 14th, 2007, 13:14
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

Yes, you're right Alex. It seems like a bug -- every time that a script/code appears more than once in the same thread, any whitespace is replaced by asterisks (except for the first time the script/code appears)...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 14th, 2007 at 13:19.
  #15 (permalink)  
Old Oct 14th, 2007, 13:19
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: PHP Printer-Friendly Page

Assuming no one else has, I've PMed an Admin.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #16 (permalink)  
Old Oct 14th, 2007, 14:22
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

I know that we've pretty much solved the problem, but I can't get over that there is not a simple way to create a print.php page that dynamically extracts all the contents of the page that is specified in the url:
HTML: Select all
http://www.domain.com/print.php?page=index.htm
Of course the 'page=' variable would change from page to page...
Rakuli, you've tried to talk me out of this, but I am going to keep fighting a bit longer. Here is a code that I found by searching Google.
PHP: Select all

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

PS: Rakuli, I know what you told me, but I don't think I want to change all of my extensions to .php.
This code is the sort of thing I originally had in mind... and I've set my sights firmly onto it.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 14th, 2007 at 14:27.
  #17 (permalink)  
Old Oct 14th, 2007, 20:32
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Printer-Friendly Page

Hold on, I think I know what is wrong: register_globals is turned off on my server...
OK, I fixed that. But now there is a new error:
HTML: Select all
Warning: join() [function.join]: Bad arguments. in /opt/www/courses2-port80/wagnerlab/print.php on line 24
Line 24 contains the following code:
PHP: Select all

$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 t