Make printing easy, using a PrintController
Posted by Felix Geisendörfer, on Sep 24, 2006 - in PHP & CakePHP » Controllers, Components & Shells
Deprecated post
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
The web site I currently work on is going to be for the hotel where my step father is the manager of. It's a beautiful little hotel in the Ore Mountains and close to Dresden, so in case you ever plan to visit the area of Germany where I live, make sure to spent a couple nights there. However, their current web site was the first big site I ever did in php (about 3-4 years ago), and is in bad need for a complete relaunch *cough*.
One of the things that is going to be an important 'feature' for the site, is to make it printer friendly. Their guests are usally no computer geeks and like to print stuff, so they can show their familiy more information about their destination.
Since the new site has a very clean markup, I thought about simply creating an additonal style sheet for the media type "print", hide the menu & other unneccessary items with it, and don't worry much about printing at all.
However, after thinking about it a little bit more today, I came to to the conclusion that I'm dealing with WYSIWYG visitors. If there are tons of graphics and a background color on the site, those people will expect to get exactly that if they hit the print button in their browser. I don't think they would mind if the printer then prints out a nice light weight CSS-free design, but I suspect them to be more likely to print things out when they know this up front.
So I decided to get a little fancier, and to create a PrintController, which would output printable versions of any site requested by /print/*. The advantage here is that you can link people to a printable version of the site, instead of telling them to trust you and your mighty CSS skills (which they wouldn't understand anyway) about not wasting their printer ink.
Ok, here is how this PrintController I came up with looks like:
-
class PrintController extends AppController
-
{
-
var $name = "Print";
-
-
function index()
-
{
-
$this->autoRender = false;
-
-
-
$Dispatcher =& new Dispatcher();
-
}
-
}
I tried to use requestAction, but I couldn't get it to return a rendered action with a given layout, so I decided to call the Dispatcher instead. In case I'm missing something, let me know.
And here a 1.1 compatible 1.2 Route to make sure /print/* works as expected.
One of the cool aspects of this method is that you can serve a different layout for your printable pages, allowing you to remove useless divities and all CSS style sheets relyably. And you can even put statements like this in your action views to optimize things even further:
-
<p>Sub Menu / Information not worth printing go in here</p>
Again, nothing to see here that couldn't be done with CSS, but personally I think it makes for a better User experience to actually see what you are going to print, even if your browser doesn't offer print preview or such.
Oh and if you are looking for a little example, here is a normal layout vs a print layout screenshot from the site I'm doing right now (and yes, the icons on the left will change again ^^).
Update:
One thing I forgot to mention: If you come from SEO-land and you are afraid google could detect duplicated content, add a robots.txt to your /app/webroot/ and put this in there:
User-agent: *
Disallow: /print
Personally I also have a "Disallow: /admin" in there - just in case ; ).
--Felix Geisendörfer aka the_undefined