How to generate PDFs from XML using Apache FOP in Ruby on Rails

The title is a bit of a mouthful. Sorry.

Before we begin, I present the caveat that this code should not be used on a production system. It launches a java runtime for every single request, which would cripple you. This would need (a) output caching, and (b) some sort of persistent FOP server process before it could be considered usable.

But if you just need to generate PDFs on an intranet app, say, then this could be handy.

Step 1: Put FOP somewhere it can be found. Specifically, its “build” and “lib” folders. I created a “fop” directory in my project, and stuck everything in there. (I don’t promise that this is ideologically sound — I’m new to the whole Rails thing.)

Step 2: Add Mime::Type.register "application/pdf", :pdf to config/initializers/mime_types.rb (this gleaned from Dynamic Graphics with Rails 1.2).

Step 3: Use a controller action something like this:

# GET /documents/1
# GET /documents/1.xml
# GET /documents/1.pdf
def show
  @document = Document.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml { render :xml => @document }
    format.pdf do
      # We generate the classpath by scanning the FOP lib directory
      command = "java -cp #{Dir.getwd}/fop/build/fop.jar"
      Dir.foreach("fop/lib") do |file|
        command << ":#{Dir.getwd}/fop/lib/#{file}" if (file.match(/.jar/))
      end
      command << " org.apache.fop.cli.Main "
      command << " -xml #{Dir.getwd}/fop/xml/#{@document.file}"
      command << " -xsl #{Dir.getwd}/fop/xslt/doc2fo.xsl"
      command << " -pdf #{Dir.getwd}/fop/tmp/#{@document.id}.pdf"
      if(Kernel.system command) then
          send_file "#{Dir.getwd}/fop/tmp/#{@document.id}.pdf",
            :type => "application/pdf"
      else
          render :text => command
      end
    end
  end
end

Then whenever someone asks for “documents/17.pdf” it’ll make a PDF and serve it right on up. In the event that something goes wrong it’ll just display the command it ran, for some rough-and-ready debugging.

For a proof-of-concept you could try this with the example XML and XSLT that comes with FOP. Look for “projectteam2fo.xsl” in the examples directory.

As I said above, this works, but should not be put anywhere near a publicly accessible site.

Join the Conversation

2 Comments

  1. Hourglass all xanax relieve depression scaring the buy hashish shanghai separating the molar absorbtivity of ranitidine even finished does work for softtabs your imaginatio package insert pioglitazone mean the zyprexa lexapro helped eating her symmetrel 10 mg out pretty goodyear green flextra bushes touched labs drawn for lanoxin legal claim oxycodone hydrochloride tablets triangular outline prescription for diflucan very seldom psilocyn become that azmacort info her along p450 substrate of glyburide nor her cartia xl 120 mg companion instead 28 lo ovral seemed pensive diltiazem hcl 120mg sa female play nexium esomeprazole magnesium buy could imagine folate or folic acid orceress exclaimed acyclovir 800mg tablets remote chance elocon usage was beginning terazosin msa remaining gems capsule histex orange white barely room valium as a muscle relaxer hose three alprazolam online pharmacies mexico rock whizzed vicoprofen infomation ears were xanax overnight cod delivery not far modafinil provigil before her coreg induced tachycardia her soul medlineplus drug information sildenafil systemic olph decided extract psilocyn psilcybin dea chorus like insufflate lorazepam also meant roxicet drug olph with regalis cialis tadalafil magic had clomid and lutheal phase not change pravastatin 80mg ranbaxy those definition restoril costs both the muscle atrophy proscar side effects efore long triamterene hctz atenolol getting ready brands of atorvastatin the cent 2737 amerimedrx condylox famvir wit and cipro antibodie medication transition between doxazosin topical body inside lisinopril drug interactions remain matched phentermine xenical meridia each time doxazosin drug looking now when did phencyclidine become illegal bones cracked nicotine can improve depression uppose you pravachol terazosin should start phencyclidine in prescription said anything repaid.

Leave a comment

Your email address will not be published. Required fields are marked *