#!/usr/bin/env ruby if ARGV.length != 3 puts "Usage: fop.rb " else $LOAD_PATH << File.join(File.expand_path(File.dirname(__FILE__)),'..','yajb') require 'yajb/jbridge' include JavaBridge fop_home = File.join(File.dirname(__FILE__),'fop-0.92beta') if !defined?(JBRIDGE_OPTIONS) then JBRIDGE_OPTIONS = {} end # NOTE:- the classpath separator is the unix one; ':', you'll want ';' for windows classpath = "$CLASSPATH:" classpath << Dir["#{File.join(fop_home, 'lib')}/*.jar"].join(':') classpath << ":#{File.join(fop_home, 'build')}/fop.jar" JBRIDGE_OPTIONS[:classpath] = classpath at_exit { JavaBridge.break_bridge } %w{org.apache.fop.apps.FopFactory org.apache.fop.apps.Fop org.apache.fop.apps.MimeConstants javax.xml.transform.TransformerFactory javax.xml.transform.stream.StreamSource javax.xml.transform.sax.SAXResult java.io.BufferedOutputStream java.io.FileOutputStream java.io.File}.each { |import_string| jimport import_string } # This is the standard xml+xsl-t pattern for using FOP. # http://xmlgraphics.apache.org/fop/0.92/embedding.html fopFactory = :FopFactory.jclass.newInstance # the output file for the pdf out = :BufferedOutputStream.jnew(:FileOutputStream.jnew(:File.jnew(ARGV.shift))) fop = fopFactory.newFop(:MimeConstants.jclass.MIME_PDF, out) factory = :TransformerFactory.jclass.newInstance # the xsl-t file to transform the xml into xsl-fo xslt = :StreamSource.jnew(:File.jnew(ARGV.shift)) transformer = factory.newTransformer(xslt) # the xmll file you want to generate a pdf from src = :StreamSource.jnew(:File.jnew(ARGV.shift)) res = :SAXResult.jnew(fop.getDefaultHandler) transformer.transform(src, res) out.close # Of course ... instead of just args on the command line for filenames # to read / output to you could use java's StringReader objects to read # from XML documents you've created with builder or some other ruby xml # generation mechanism... end