Export to .doc or pdf
Hey guys -
I have a page with many CSS elements that I would like to add some functionality to. I would like to provide the option for the user to print the page, as well as export the page as a pdf or .doc. The page presents data from a database in both list and table form.
Any tips? Thanks for your time :eye:
I've done this a few times ( Mostly from php! ) , they are tons of stuff on the net on how to do it,
Just to start you off ( i cannot post links so just search google for the following ) :
Pdf: PDF::Writer for Ruby
Get reading :)
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:
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.
<%= stylesheet_link_tag 'standard_style_sheet' %>
<%= stylesheet_link_tag 'print_style_sheet', :media => "print" %>
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
And reverse it in the 'standard_style_sheet'.
/* Display print_only elements */
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.
/* Display no_print elements */
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:
<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>
<p>Please sign here:</p>
I didn't expect THAT much help...:lol: thanks so much!!
I've been looking at this too, as I'll probably have to provide both formats. While you can create a PDF document that way, I don't think you can convert an HTML document with CSS into a PDF document in Ruby at all. Not with decent CSS support anyways. It looks like I'm going to have to use something from another language. Depending on the type of CSS support you'll need (very good or poor), it could be tricky.