Print-friendly pages require either code forking or magic tricks with CSS, right? Not with Rails. You barely have to modify your code to make this work. You will need to be using Rails’ web service support with respond_to blocks.

Fun part first. Just add a line to your respond_to block, like this:

1
2
3
4
respond_to do |format|
  format.html
  format.print { render :layout => "print" }
end

Next, you’ll need a layout for your print-friendly page. Create a new layout called print.rhtml, and create the associated print stylesheet(s). I’ll wait while you do that.

Now you’ll need to add a mime type for the print format. You can register a new mime type with your server. But a quicker way to do this is to add this line to your environment.rb file:


Mime::Type.register "text/html", :print

Make sure to restart your application so that the mime type is actually loaded.

Now the money shot – add a link to your new print-friendly page: