Forum Moderators: phranque
The print style sheets look appealing but from what I can tell they only work in certain browsers/versions. I am really looking for something that will work for all users.
There is only one way to do this if you really mean "all users" unless you want to get into duynamic content generation based on user_agent...
...if you are using a dynamic site you could set up a seperate CNAME of print.domain.com (or apend print to a URI thus - foo.com/print/previous/uri.foo - and any request coming into that insert only the text - formatted if you wish - for the specific URI. I used this on a site for a client that wanted it to work for everyone/did not want CSS.
www.foo.com/articles/topic.html
...became
print.foo.com/articles/topic.html
OR
www.foo.com/print/articles/topic.html
It sort of works remotely too, so if you want all your pages branded HP that could be a very easy solution ;)
Stretch
The print style sheets look appealing but from what I can tell they only work in certain browsers/versions. I am really looking for something that will work for all users.
Well it depends on how complicated you want to get. If you just want the user to print the text content and a company logo/identification its not to difficult. Regarding browser support at worst, its going to look as bad as just printing the page normally. The major browsers should all support the basic things you want to accomplish with a print style sheet, like removing images, navigation, footer etc.
I just control page width with elements on the online page unless its going to be liquid .. and then remove things like menus from the printed page ....
allowing printed pages to liquidly resize width wise according to US Letter or EU A4 page formats ..
Bear in mind that you may not be able to control the settings which some users have in their browsers as relate to printing pages from the web .. bg images / colour in IE etc come to mind
Its actually one of the most useful things I learnt in here the last time I spent time in here. I use it on most sites now and most of my customers had never really considered it but like that I have.
How many of your target users are using browsers which will not support this?
it seems harder to me to control page lengths between pages etc so I dont bother with that
I know what you mean. It seems like it should be easy enough to do but I dont think the page-break-before/after etc. properties work the way they are supposed to. But hey printing from browsers has always produced wacky results so I dont think users are too concerned about it.
Every printer driver was different and was causing variable bottom margins. I figured out a way to let the user determine their optimal page break space, and tweak accordingly. Once they found their "number" they click on "Save" and the DB records their number. Then, the next time they need to print labels from that printer, it works perfectly every time.
That was my last print from browser issue and I haven't heard complaints about it yet.
What I dont know .. is how search engines take the permanent presence of a link to a printer page which contains essentially the same textual information, perhaps even better optimised for leaving out a lot of formatting code :-)
It has struck me in the past that it could (if I knew exactly what I was doing with it .. and at the moment I am not 100% sure) prove to be a booster to rankings on some terms if you let them index it :-) .. but if wrong could of course be taken as duplicate content.
Perhaps I will have to set up a test for that sometime .... unless someone already knows what the likely results are?
For other pages I will use a css that includes the @media print command and hide things like logos and excess drop down boxes from the browser when printing.
Works well.
Pass the URL of the requested page into a script as a GET querystring.
Load the output of the passed URL into a string variable. This means you use HTTP to parse the file, so you get the output from all your fancy database queries, not just a bunch of source code.
in PHP:
$fh = fopen($filename,"r")
If your site uses a template, put some convenient comments in the template to show the beginning and end of your "printable stuff". This lets you isolate and get rid of the header, banner ads, and other junk.
Grab the <title> from the page and make it the title for your printable page as well.
this can be done with:
preg_match('{<title>(.*)<\/title>}i',$page,$out)
Use a series of Regular Expressions to replace naughty bits:
1) replace <a>'s with <u>'s. Actually, don't use <u> - use a <span> instead. The cascading will retain existing formatting while adding the underline. <u> doesn't do that.
in PHP:
$inputstring = preg_replace('/<a\s+[^>]*>/','<SPAN STYLE="TEXT-DECORATION:underline">',$inputstring);
$inputstring = preg_replace('/\<\/a>/i','</span>',$inputstring);
2) remove all embedded objects, if necessary
3) remove all <scripts>
$searchstr = '{'.preg_quote('<script').'[\s\S]*?'.preg_quote('</script>').'}i';
$replacestr = '';
$inputstring = preg_replace($searchstr,$replacestr,$inputstring);
4) Rewire all the dependent links and images. Don't forget that they may appear as "src" or "background" attributes
(this is incomplete code that won't necesarily work on your site, but should offer a head start)
// find relative HTML entities using the "src" attribute
if (preg_match_all('/src=[\'\"](.*?)[\'\"]/i',$inputstring,$out,PREG_PATTERN_ORDER)){
$imgs = array_unique($out[1]);
foreach($imgs as $key => $value){
$searchstr = '{'.preg_quote($value).'}i';
$replacestr = "../".$relativepath.$value;
$inputstring = preg_replace($searchstr,$replacestr,$inputstring);
}
}
5) replace the link to your normal CSS with a link to a modified CSS
add your convenient print button...
<a href="javascript:window.print()">print this page</a>
And you're all done!