Forum Moderators: phranque

Message Too Old, No Replies

Printable Versions of HTML pages

any good solutions?

         

webbguy

8:53 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



Does anyone know of a good solution for offering printable versions of HTML pages. I just began working on a large site that does not use CSS (I hope to begin to convert the site over to CSS in the relatively near future). I am wondering if there is a print only type of solution we can implement right now on the site that strips out the graphics, etc.

Reflection

9:45 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



You could probably remove your graphics by using css without too much difficulty. Check out the css forum, there are lots of threads on print style sheets.

webbguy

7:37 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



Thanks for your post Reflection. Sorry I didn't follow-up sooner but I am happy to say I had a long weekend away from the computer.

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.

forefront

7:47 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



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

Stretch

8:33 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



HP use an interesting solution. When you click on 'printable version' they run a script that creates a new page with only the information contained between two specific comment tags. I don't imagine it would be too hard to write a similar script if that's an option.

It sort of works remotely too, so if you want all your pages branded HP that could be a very easy solution ;)

Stretch

txbakers

8:42 pm on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a link to a "printable" version, which is nothing but the bare bones data.

dragonlady7

8:54 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



I'm going to have to do the really clunky solution and only offer printing on certain specific pages, and do it via PDF.
I know, ugly, slow, awful, but it works in our specific instance because it's product brochures and we always distribute them this way. Look at it this way-- at least we have a real actual true HTML site to begin with.
:/

Reflection

5:27 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



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.

Mark_A

5:38 pm on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I find the print stylesheet code to work fine .... its easy to control page width .. removing graphic or nav elements not needed for the printed page and add copyright notes etc .. it seems harder to me to control page lengths between pages etc so I dont bother with that ....

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?

Reflection

5:49 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



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.

txbakers

8:24 pm on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was able to solve mailing labels from the browser very nicely, with some user help.

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.

Mark_A

3:58 am on Aug 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi txbakers I think it is a bit different if the site is dynamic and you can generate the printable page each time in response to user requests because you could, as you have, offer more than a one size fits all .... the css solutions are a one time and one size fits all type of thing in comparison.

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?

txbakers

4:25 am on Aug 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My sites are all dynamic. For content that is meant to be printable, such as forms, reports and the like, I have a printable format pre-built into the design.

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.

httpwebwitch

8:23 pm on Sep 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best method for creating a "printable" page. Sorry if I'm obtuse, since I'm trying to explain it briefly.

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!