[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.



 Subscribe in a reader

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

Notices


Closed Thread
 
LinkBack Thread Tools
  #1  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 14th, 2007, 02:01
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #12  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #13  
Old Oct 14th, 2007, 13:00
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #14  
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #15  
Old Oct 14th, 2007, 13:19
alexgeek's Avatar
Moderator