Jan 19

Rails Pdf Plugin act_as_flying_saucer

There are various ways to generate pdf documents in any language.In Rails we can use prawn library ,HtmlDoc,PrinceXml and many other library,using their api we can generate pdf document.Basically the primary goal is converting HTML web pages to PDF Document,without much changing existing CSS and HTML.

Using the Flying Saucer Project we can achieve this.we can convert HTML + CSS to PDF documents without much changing HTML and CSS. It also support CSS 2 and many properties of CSS 3 for printing Header,Footer,Page Number,Paginated Tables and many more.This Project is built on Java so Java is required on system.

Lets Start How to generate PDF Using Flying Saucer.

 script/plugin install git://github.com/amardaxini/acts_as_flying_saucer.git

This Plugin forked From


Which has older version of flying saucer project.

Next Step after installing plugin add flying_saucer.rb file at config/initializers.

ActsAsFlyingSaucer::Config.options = {
:java_bin => "/usr/bin/java",          # java binary
:classpath_separator => ':',  # classpath separator. unixes system use ':' and windows ';'
:tmp_path => "/tmp",          # path where temporary files will be stored

After Setting Java path , classpath separator and tmp path now do following step.

class PdfController < ActionController::Base

  def generate_pdf
    render_pdf :template => 'pdf/pdf_template'
  render_pdf :file=>'pdf/generate_pdf.html.erb'
  render_pdf :template=>'pdf/generate_pdf.pdf.erb',
             :send_file => { :filename => 'pdfdoc.pdf' }

Add act_as_flying_saucer to controller then render_pdf.There are various ways to render pdf using “template” or “file“,:send_file option use to send pdf file to client side.if we are using any external css file then this css shoulda have media option as ‘print’.

<%= stylesheet_link_tag 'pdf' ,:media=>'print' %>

If pdf containing images which generate on the fly.so instad of using

<%= image_tag %>


 <img src=" " >

Before rendering PDF validate HTML,like whether all tags are close properly or not.otherwise it gives an parse error.

Now You are ready to Generate PDF. Hurray!!!!!!!!!

How to generate Header, Footer,Page Number and Automation Of HTML Validation will be discussed in next post.

Be Sociable, Share!

8 Responses to “Generate PDF Using Rails and Flying Saucer”

  1. Heinrich says:

    “How to generate Header, Footer,Page Number and Automation Of HTML Validation will be discussed in next post.”

    Dude, did you ever wrote this post? 😀


  2. Jussi says:

    Could you help me with the following error? My platform is OS X 10.6.6 and Java 1.6.x.

    Exception in thread “main” java.lang.NoClassDefFoundError: Data/NetBeansProjects/pdftesti/vendor/plugins/acts_as_flying_saucer/lib/java/jar/acts_as_flying_saucer/jar
    Caused by: java.lang.ClassNotFoundException: Data.NetBeansProjects.pdftesti.vendor.plugins.acts_as_flying_saucer.lib.java.jar.acts_as_flying_saucer.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

  3. nobleach says:

    I got this going just fine on my local dev box. As soon as I deploy, it renders the pdf just fine from HTML. (I can view it in the /tmp directory, and it looks fine).

    When it sends it to the browser, it’s an empty (0 Byte) file… and thus Adobe Acrobat can’t open it.

    Something odd is happening between the rendering and the sending of the file.

    There is VERY little in the way of documentation or help on this project.

  4. Amar Daxini says:

    I think your Html is not proper,May be some of the tags is not properly close.Make sure before generating PDF XHTMLis Proper.

  5. nobleach says:

    That is exactly what the problem was. Now I’m having trouble moving from our overly taxed linux server to a windows machine.

    I just get:

    ActionController::MissingFile in ProjectsController#pdf

    Cannot read file c:/tmp/ff684852ff21574731f93747e94cc4e3.pdf

    The HTML file goes to the c:\tmp directory… but it never generates a PDF version of it.

  6. jparedes says:

    The same problem nobleach reported. In a linux machine works fine, but in a windows machine, just generates the html file.

    My initializer’s file

    ActsAsFlyingSaucer::Config.options = {
    :java_bin => “C:\\Archivos de programa\\Java\\jre1.5.0_22\\bin\\java”,
    :classpath_separator => ‘;’,
    :tmp_path => “#{RAILS_ROOT}/public/uploads/temp”

  7. bgrant says:

    Out of curiosity, as I’m having the same 0-byte issue on our server, why would malformed xhtml work when run locally but the same input file cause the empty PDF sometimes on the server?

  8. viren says:

    Is java is installed properly,
    Checkout XHTML well form ness try :clean =>true option and again check.

Leave a Reply

preload preload preload