For the "print page" option, the best policy is to use a dedicated print CSS. You can call more than one style sheet in your HTML header. If you have two and one has the "media" attribute set to 'print', that one will be used when printing and the other in normal viewing. Rails gives you the tools to do that easily:
Code:
<%= stylesheet_link_tag 'standard_style_sheet' %>
<%= stylesheet_link_tag 'print_style_sheet', :media => "print" %>
Put that in a the header of a layout and the 'standard_style_sheet' css file will be used for screen, and 'print_style_sheet' when printing. Of course you'd rename those to match the controller name in line with normal Rails conventions.
It's then worth adding no_print and print_only class definitions to your style sheets.
In 'print_style_sheet' you'd do something like this
Code:
.no_print {
display: none;
}
.print_only {
/* Display print_only elements */
}
And reverse it in the 'standard_style_sheet'.
Code:
.no_print {
/* Display no_print elements */
}
.print_only {
display: none;
}
Note that in HTML, class="one two" equates to 'this element belongs to the class "one" AND the class "two". Class names separated by spaces act as a list of classes the element belongs to. So changing an element's class definition from class="navigation" to class="navigation no_print" means it will continue to be modified by CSS directives set against .navigation, but now it will also be modified by directives set against .no_print.
You can then choose which HTML elements to have in each view. For example you may want to hide navigation links in the print view and give someone a sign off box to add a signature to the print out:
Code:
<a href="#bottom" class="no_print">Go to bottom</a>
<p>A bit of text that will appear in both screen and print fomats</p>
<div class="print_only">
<p>Please sign here:</p>
<p>__________</p>
</div>
Also:
Code:
<% link_to_function("Print Page", "javascript:print()") %>
Will give you a print link. However, for most occasions users can simply use the normal browser print options. That's all that piece of JavaScript is calling. It not doing anything particularly special.
Bookmarks