Generate PDF Using Rails and Flying Saucer

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://

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 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.

9 comments On Generate PDF Using Rails and Flying Saucer

  • “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? 😀


  • 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:
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(

  • 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.

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

  • 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.

  • 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”

  • 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?

  • Hi everybody, I know it is a dead thread but today I face the same problem:

    Cannot read file tmp/172305971479241292730.pdf.

    Is there someone who can help??

    Thanks in advance guys

Leave a reply:

Your email address will not be published.

Site Footer