Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a printer friendly HTML page?

How do I change or remove layouts from my application on a specific Rails view to allow my users to have a more enjoyable printing experience? (No navbar, or other extra data)

I have been banging my head against the table for a few hours now trying to figure this out. I have tried using render layout: false with no success.

I have tried creating an action like below but it didn't work for me either:

def print
  respond_to do |format|
    format.html { render layout: false } 
  end
end

I tried linking to this action by using the following:

<%= link_to 'Printer Friendly Version', product_path(@product), :action => 'print', target: '_new' %>

Am I just completely off base here? How can I better approach this obstacle? I do not want to use PDF or Javascript, I just want to render printer-friendly HTML.

like image 680
pyRabbit Avatar asked May 21 '14 19:05

pyRabbit


1 Answers

You can do it just with CSS like this: See full article here.

<style type="text/css">
@media print{
  body{ background-color:#FFFFFF; background-image:none; color:#000000 }
  #ad{ display:none;}
  #leftbar{ display:none;}
  #contentarea{ width:100%;}
}
</style>

It uses a simplified CSS when it detects that the page is being sent to the printer.

-- W3Schools link --.

Also see this article:

Abstract: How to make printer friendly web pages

If your web site delivers value to your audience, chances are they are going to want to print pages out. Making sure desktop printers handle your web site well is another aspect of building a great user-experience.

This article takes a quick look at using Cascading Style Sheets to design printed web pages.

like image 68
Paul Sasik Avatar answered Sep 21 '22 19:09

Paul Sasik