RubyPDF Blog English,iText,JRuby on Rails(JROR),PDF HelloWord.rb:Using iText & JRuby

HelloWord.rb:Using iText & JRuby

JRuby is the effort to recreate the Ruby interpreter in Java. JRuby is tightly integrated with Java to allow both to script any Java class and to embed the interpreter into any Java application.


iText is a library that allows you to generate PDF files on the fly.
The iText classes are very useful for people who need to generate read-only, platform independent documents containing text, lists, tables and images. The library is especially useful in combination with Java(TM) technology-based Servlets: The look and feel of HTML is browser dependent; with iText and PDF you can control exactly how your servlet’s output will look.

So how about Creating and Manipulating PDF by using iText & JRuby?
The following is a HelloWorld example ported from the iText

require 'java'

include_class ["FileOutputStream","IOException"].map {|e| ""+e}
include_class ["Document","Paragraph","DocumentException"].map {|e| "com.lowagie.text."+e}
include_class "com.lowagie.text.pdf.PdfWriter"

class HelloWorld 

	#def main
		p("Hello World")

		# step 1: creation of a document-object
			# step 2:
			# we create a writer that listens to the document
			# and directs a PDF-stream to a file

			#step 3: we open the document
			#p document
			#step 4: we add a paragraph to the document
			document.add("Hello World"))
		rescue DocumentException
			$stderr.print "Document failed: " + $!
		rescue IOException

When I run it, I got the error:

"Hello World"
Document failed: com.lowagie.text.DocumentException: The document is not open ye
t; you can only add Meta information.HelloWorld.rb:33:in `close': com.lowagie.te
xt.ExceptionConverter: The document is not open. (NativeException)
from HelloWorld.rb:33
from :-1
Complete Java stackTrace
java.lang.RuntimeException: The document is not open.
at com.lowagie.text.pdf.PdfWriter.getDirectContent(
at com.lowagie.text.pdf.PdfDocument.newPage(
at com.lowagie.text.pdf.PdfDocument.close(
at com.lowagie.text.Document.close(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.

That means does not work. what is the matter? I tried to debug this example in jirb, when wrote down and hit return, the result is false.
So I decide do some hack on iText, clone open method of Document but with a different method name”doOpen”.
and also replace “” with “document.doOpen()“,
jruby HelloWorld.rb, it work now.
I do not know why, but will try to find it.

the iText I used is v1.4.5, jRuby is 0.9.0
the usage is:
d:>set CLASSPATH=%CP%;%CLASSPATH%;itext-1.4.5.jar
d:>jruby HelloWorld.rb

2 thoughts on “HelloWord.rb:Using iText & JRuby”

  1. My guess is that the open method is colliding with Kernel#open. Of course it should not; classes, including those imported from Java, should be able to define their own open methods. If you can narrow down that this is the case, could you report it in our JIRA? (google “jira jruby”).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.