JRuby Fusion

I recently ported a medium-sized Java web application to Ruby on Rails. Overall it was a gratifying experience but I struggled with PDF generation, and specifically the limits of the Ruby PDF generator PDF::Writer and associated SimpleTable.

Actually PDF::Writer itself is fairly powerful but unfortunately SimpleTable lives up to its name by lacking many of the features found in other PDF generators.

My original Java app uses the awesome iText PDF generator which has extensive table support.

After fiddling with SimpleTable for a while, I gave up and wrote a simple bridge between my Rails app and my original Java PDF generation servlet. On the rails side I create a delimited string containing each record to be printed, and post that to the servlet with:

    http.post(path_to_servlet, delimited_string)

On the Java side I parse the InputStream to retrieve the values, and generate table cells using the iText APIs.

This is straightforward, but it bugs me having to maintain two codebases in two languages, and deploy two separate applications, one in an app server, to support PDF generation. It would be far preferable to access iText directly from Ruby.

Enter JRuby

So I installed JRuby on my S10 box, and entered these two commands (courtesy Charles Nutter):

  % gem install glassfish-gem-10.0-SNAPSHOT.gem
  % glassfish_rails myapp

And voila: my app is running inside Glassfish on JRuby. Wow - that was easy! I can get rid of my mongrels and manage everything from GlassFish. Even better, I can now call Java from within ruby:

setenv CLASSPATH itext.jar

require 'java'

include_class 'java.io.FileOutputStream'
include_class 'com.lowagie.text.Document'

document = Document.new(PageSize::A4.rotate(), 10, 10, 10, 10)
pdfFile = FileOutputStream.new("it_works.pdf")

writer = PdfWriter.getInstance(document, pdfFile)

table = PdfPTable.new(9)

This is the best of all worlds. Of course there are some downsides with this approach, such as inevitable impedance mismatches between Java and Ruby, code maintainability issues, etc. but this approach quickly and elegantly solved my original problem of not being able to access iText.

First impressions: JRuby Rocks.

Comments:

Nice post, and this is a bit besides the point, but I hear all the cool kids are using Prawn these days.

http://prawn.majesticseacreature.com/

Posted by Mike on March 17, 2009 at 09:41 PM PDT #

I've used the flying saucer project in a jruby rails app filter to generate a pdf when the format is pdf.

https://xhtmlrenderer.dev.java.net/

Then you get an html and pdf view with just your html! JRuby is awesome.

Looks like whitespace is messed up but here's one way to do it...

module FlyingSaucer
module IText

class Filter

include_class javax.xml.parsers.DocumentBuilderFactory;
include_class org.xhtmlrenderer.pdf.ITextRenderer;
include_class java.io.StringBufferInputStream
include_class java.io.ByteArrayOutputStream

def self.filter(controller)
return unless controller.params[:format] == "pdf"
begin
controller.response.content_type = "application/pdf"
uri = URI.parse(controller.request.env['REQUEST_URI'])
controller.response.body = create_pdf(controller.response.body,"#{uri.scheme}://#{uri.host}:#{uri.port}#{File.dirname(uri.path)}")
rescue
controller.logger.warn "could not render pdf with body #{controller.response.body}"
raise
end
end

def self.create_pdf(str, url = nil)
renderer = ITextRenderer.new
renderer.set_document(DocumentBuilderFactory.new_instance.new_document_builder.parse(StringBufferInputStream.new(str)), url)
renderer.layout
out = ByteArrayOutputStream.new
renderer.createPDF(out)
out.flush
out.close
String.from_java_bytes(out.to_byte_array)
end
end
end
end

Posted by Archie on March 17, 2009 at 10:07 PM PDT #

Hello,

Please add your site at http://www.sweebs.com. Sweebs.com is a place where other people can find you among the best sites on the internet!
Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.

Regards
Kris

Posted by Kris on March 18, 2009 at 06:00 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

john

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today