Friday 8 January 2010

Playing with iText

hurrah for Hello World

In the app - I need to create a whole bunch of pdfs and then glue them all together. Having installed iText and RJB seems sensible to try and get it to work.
Playing in the console - the following actually worked

>> require 'rjb'
=> ["RjbConf"]
>> Rjb::load('/var/www/html/renaissance/lib/iText.jar')
=> nil
>> filestream = Rjb::import('java.io.FileOutputStream')
=> #
>> pdfreader = Rjb::import('com.itextpdf.text.pdf.PdfReader')
=> #
>> pdfwriter = Rjb::import('com.itextpdf.text.pdf.PdfWriter')
(irb):5: warning: already initialized constant NAME
=> #
>> pdfdocument = Rjb::import('com.itextpdf.text.Document')
=> #
>> document = pdfdocument.new
=> #<#:0x2b49dc62af10>
>> pdfwriter.getInstance(document, filestream.new('/tmp/test.pdf'))
=> #<#:0x2b49dc624430>
>> document.open
=> nil
>> pdfParagraph = Rjb::import('com.itextpdf.text.Paragraph')
=> #
>> paragraph = pdfParagraph.new("hello world")
=> #<#:0x2b49dc60cb78>
>> document.add(paragraph)
=> true
>> document.close
=> nil
>>


It opens a for real pdf with the iconic words "Hello World". Hurrah

No comments:

Post a Comment