# Java Bridge Itext Example Anyone?

**URL:** <https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263>\
**Category:** Rails\
**Created:** [July 5, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263 "2008-07-05T14:39:36Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 5, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/1 "2008-07-05T14:39:36Z")

</div>

I MAY be able to derive something out of the present example given here  
:  
[http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/](http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/)

but this deals with filling out forms. Is there a simpler example that  
just allows you to talk to itext , send it some plain text and get back  
a pdf and then send that pdf to the user as downloadable / renderable  
data?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 5, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/2 "2008-07-05T14:54:01Z")

</div>

On Sat, 2008-07-05 at 14:39 +0200, Ather S. wrote:

> I MAY be able to derive something out of the present example given here  
> :  
> [http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/](http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/)
> 
> but this deals with filling out forms. Is there a simpler example that  
> just allows you to talk to itext , send it some plain text and get back  
> a pdf and then send that pdf to the user as downloadable / renderable  
> data?

* * *

sounds like all you really want is pdf-writer

itext is for filling out forms (and by the way, I ended up using a  
combination of pdf-stamper gem and some more raw methods because  
rjb/itext was incapable of doing checkboxes).

see [http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs](http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs)

Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 5, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/3 "2008-07-05T17:19:25Z")

</div>

No I do not want pdfwriter because I already tried it and it is  
inadequate. It does not have the capabilities I want like putting  
pushbuttons on the pdf or encrypting them (pdf writer’s encryption is  
broken and removed) but for starters an example would have been nice.

Craig W. wrote:

> On Sat, 2008-07-05 at 14:39 +0200, Ather S. wrote:
> 
> > I MAY be able to derive something out of the present example given here  
> > :  
> > [http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/](http://blog.codeinmotion.com/index.php/2006/12/22/pdf-generation-in-ruby-on-rails/)
> > 
> > but this deals with filling out forms. Is there a simpler example that  
> > just allows you to talk to itext , send it some plain text and get back  
> > a pdf and then send that pdf to the user as downloadable / renderable  
> > data?
> 
> * * *
> 
> sounds like all you really want is pdf-writer
> 
> itext is for filling out forms (and by the way, I ended up using a  
> combination of pdf-stamper gem and some more raw methods because  
> rjb/itext was incapable of doing checkboxes).
> 
> see [http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs](http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs)
> 
> Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 5, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/4 "2008-07-05T18:12:59Z")

</div>

Are you following my question ? Or do you like to come up with  
tangential issues ?

I will write up a simple example and put it up myself 🙂

Craig W. wrote:

> pdf-writer comes with a chunky bacon example if I recall correctly.
> 
> You could always ‘post-process’ a pdf file with pdftk to encrypt it.
> 
> Don’t know about pushbuttons in pdf’s
> 
> As far as I know, the rails wiki page on PDF that I linked in the  
> previous e-mail is the exhaustive resource on integrating PDF’s with  
> rails.
> 
> Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 5, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/5 "2008-07-05T17:27:54Z")

</div>

pdf-writer comes with a chunky bacon example if I recall correctly.

You could always ‘post-process’ a pdf file with pdftk to encrypt it.

Don’t know about pushbuttons in pdf’s

As far as I know, the rails wiki page on PDF that I linked in the  
previous e-mail is the exhaustive resource on integrating PDF’s with  
rails.

Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 6, 2008, 11:59am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/6 "2008-07-06T11:59:15Z")

</div>

require ‘rjb’  
Rjb::load(‘iText-2.1.2u.jar’)

filestream = Rjb::import(‘java.io.FileOutputStream’)

pdf\_writer = Rjb::import(“com.lowagie.text.pdf.PdfWriter”)

document = Rjb::import(“com.lowagie.text.Document”)

par = Rjb::import(“com.lowagie.text.Paragraph”)

ps = Rjb::import(“com.lowagie.text.PageSize”)

chunk = Rjb::import(“com.lowagie.text.Chunk”)  
font = Rjb::import(“com.lowagie.text.Font”)

#ff = Rjb::import(“com.lowagie.text.FontFactory”)  
bf = Rjb::import(“com.lowagie.text.pdf.BaseFont”)

#ff.register(“C:\WINDOWS\Fonts\CALIBRIB.TTF”, “calibri”);

arial =  
bf.createFont(  
“C:\WINDOWS\Fonts\CALIBRIB.TTF”,  
bf.IDENTITY\_H,  
bf.NOT\_EMBEDDED);

m = filestream.new(“asdasd.pdf”)

```
document= document.new()

writer= pdf_writer.getInstance(document,m)

#writer.closeStream = false

document.open()

document.add(par.new("Hello World!", font.new(arial)))

document.add(par.new("Now I run on Rails!", font.new(arial)))
document.add(par.new("àÎÉËÏÄ ÐÁÛÅÔ!!!!", font.new(arial)))

document.close

```

m.close

On Jul 5, 4:39špm, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 6, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/7 "2008-07-06T17:15:50Z")

</div>

Cool that is what I was looking for … I have some of it abstracted to  
a java class; Do I send m to the render method ?

miv wrote:

> require ‘rjb’  
> Rjb::load(‘iText-2.1.2u.jar’)  
> #ff.register(“C:\WINDOWS\Fonts\CALIBRIB.TTF”, “calibri”);  
> arial =  
> bf.createFont(  
> “C:\WINDOWS\Fonts\CALIBRIB.TTF”,  
> bf.IDENTITY\_H,  
> bf.NOT\_EMBEDDED);  
> m = filestream.new(“asdasd.pdf”)  
> document= document.new()  
> writer= pdf\_writer.getInstance(document,m)  
> #writer.closeStream = false  
> document.open()  
> document.add(par.new(“Hello World!”, font.new(arial)))  
> document.close  
> m.close

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 4:15am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/8 "2008-07-07T04:15:50Z")

</div>

RJB cant seem to be able to load iText-2.1.2u.jar; where is this placed?  
I tried placing it the c:/ruby/lib folder  
then in the lib folder and then in the folder where I was running irb ;  
then in the jdk’s lib and still was unable to locate this jar. it keeps  
returning nil;

and when I call chunk for example it gives me classdefnotfound etc.

miv wrote:

> require ‘rjb’  
> Rjb::load(‘iText-2.1.2u.jar’)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 4:54am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/9 "2008-07-07T04:54:23Z")

</div>

Craig W. wrote:

> On Mon, 2008-07-07 at 04:15 +0200, Ather S. wrote:
> 
> > > require ‘rjb’  
> > > Rjb::load(‘iText-2.1.2u.jar’)
> 
> * * *
> 
> I use the pdf-stamper gem and it incorporates itext with the gem (in the  
> ext folder)
> 
> Craig

You expect me to install pdf-stamper even though i am not using pdf  
stamper? and I should install pdfstamper just so I can get to the itext  
libraries?  
I dont think that is a good idea, I dont know what everyone else thinks  
about this.

I tried putting the jars in rjb’s ext folder but that didnt work.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 4:39am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/10 "2008-07-07T04:39:02Z")

</div>

On Mon, 2008-07-07 at 04:15 +0200, Ather S. wrote:

> > require ‘rjb’  
> > Rjb::load(‘iText-2.1.2u.jar’)

* * *

I use the pdf-stamper gem and it incorporates itext with the gem (in the  
ext folder)

Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 5:08am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/11 "2008-07-07T05:08:52Z")

</div>

On Mon, 2008-07-07 at 04:54 +0200, Ather S. wrote:

> You expect me to install pdf-stamper even though i am not using pdf  
> stamper? and I should install pdfstamper just so I can get to the itext  
> libraries?  
> I dont think that is a good idea, I dont know what everyone else thinks  
> about this.
> 
> I tried putting the jars in rjb’s ext folder but that didnt work.

* * *

no - I don’t expect you to do anything. I’m just relating that the  
pdf-stamper gem automatically brings the itext.jar in with it and it is  
automatically loaded when you load PDF::Stamper.

I suppose you could load the gem just to see how that gem makes use of  
it but the choice is yours.

The gem loads it like this…  
Rjb::load(File.join(File.dirname( **FILE** ), ‘…’, ‘…’, ‘ext’,  
‘itext-2.0.4.jar’))

Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 7:46am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/12 "2008-07-07T07:46:46Z")

</div>

I placed jar in the same folder as script. I think any folder in PATH  
will do. Does any other java lib working? may be problem in jvm  
version?

On 7 ÉÀÌ, 06:15, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/13 "2008-07-07T13:31:30Z")

</div>

Yes String is working for example from the irb interface.  
I have placed the jar file in script but when I type  
Rjb::load(‘iText-2.1.2u.jar’) I get an error saying  
RuntimeError : unknown error  
from (irb):2: in ‘initialize’  
from (irb):2: in ‘dlopen’  
from (irb):2: in ‘load’

miv wrote:

> I placed jar in the same folder as script. I think any folder in PATH  
> will do. Does any other java lib working? may be problem in jvm  
> version?
> 
> On 7 , 06:15, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 7, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/14 "2008-07-07T23:46:45Z")

</div>

Is there a way of sending this pdf file asdasd.pdf to the browser ; Send  
binary data to the user as a file download as follows :

send\_data(BINARY\_DATA,{:filename=\>“asasd.pdf”,:type=\>“application/pdf”})

send\_data at :

> **[ActionController::Streaming](https://api.rubyonrails.org/classes/ActionController/Streaming.html)**
>
> Allows views to be streamed back to the client as they are rendered. By default, Rails renders views by first rendering the template and then the layout.

What is BINARY\_DATA below? is it document, m, pwf\_writer?

BTW I am still unable to load this jar file using rjb … you are  
welcome to see here :  
[http://rubyforge.org/forum/forum.php?thread\_id=26875&forum\_id=8190](http://rubyforge.org/forum/forum.php?thread_id=26875&forum_id=8190)

miv wrote:

> require ‘rjb’  
> Rjb::load(‘iText-2.1.2u.jar’)  
> filestream = Rjb::import(‘java.io.FileOutputStream’)  
> pdf\_writer = Rjb::import(“com.lowagie.text.pdf.PdfWriter”)\>  
> document = Rjb::import(“com.lowagie.text.Document”)  
> m = filestream.new(“asdasd.pdf”)  
> document= document.new()  
> writer= pdf\_writer.getInstance(document,m)  
> document.open()  
> document.add(par.new(“Hello World!”, font.new(arial)))  
> document.add(par.new(“Now I run on Rails!”, font.new(arial)))  
> document.close  
> m.close

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 14, 2008, 3:43am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/15 "2008-07-14T03:43:07Z")

</div>

On Mon, 2008-07-14 at 03:14 +0200, Ather S. wrote:

> last line of the above blog (that is I can generate the document and  
> I try to do a byte[].toString() and send that to send\_data but when I  
> get byte[].toString() from the java classes I get an error :
> 
> No Class DefFoundError (org/bouncycastle/asn1/ASN1OctetString)
> 
> Where to go from here?
> 
> From the blog : Is there an equivalent to String.from\_java\_bytes in  
> ruby?

* * *

I can’t help here except that I save the PDF to a file and then use  
send\_file and it’s all fairly simple.

Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 14, 2008, 6:17am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/16 "2008-07-14T06:17:49Z")

</div>

Craig W. wrote:

> On Mon, 2008-07-14 at 03:14 +0200, Ather S. wrote:
> 
> > last line of the above blog (that is I can generate the document and  
> > I try to do a byte[].toString() and send that to send\_data but when I  
> > get byte[].toString() from the java classes I get an error :
> > 
> > No Class DefFoundError (org/bouncycastle/asn1/ASN1OctetString)
> > 
> > Where to go from here?
> > 
> > From the blog : Is there an equivalent to String.from\_java\_bytes in  
> > ruby?
> 
> * * *
> 
> I can’t help here except that I save the PDF to a file and then use  
> send\_file and it’s all fairly simple.
> 
> Craig

OK!! that works … I am at a point where this maybe inefficient but I  
will take it! Thanks !

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 14, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/17 "2008-07-14T17:53:55Z")

</div>

You can use send\_data by passing it a bytearray!!! no need for  
String.fromjavabytes…

Craig W. wrote:

> On Mon, 2008-07-14 at 03:14 +0200, Ather S. wrote:
> 
> > last line of the above blog (that is I can generate the document and  
> > I try to do a byte[].toString() and send that to send\_data but when I  
> > get byte[].toString() from the java classes I get an error :
> > 
> > No Class DefFoundError (org/bouncycastle/asn1/ASN1OctetString)
> > 
> > Where to go from here?
> > 
> > From the blog : Is there an equivalent to String.from\_java\_bytes in  
> > ruby?
> 
> * * *
> 
> I can’t help here except that I save the PDF to a file and then use  
> send\_file and it’s all fairly simple.
> 
> Craig

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 14, 2008, 3:14am UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/18 "2008-07-14T03:14:25Z")

</div>

I am still attempting to figure out how to send an Itext generated pdf  
Document to the browser.

I am looking at the following blog posting where someone is using jruby  
and iText to create a pdf file and then sending it to the browser :  
[http://codersifu.blogspot.com/2007/06/howto-generate-pdf-files-in-ruby-on.html](http://codersifu.blogspot.com/2007/06/howto-generate-pdf-files-in-ruby-on.html)

Unlike the above example I am using only ruby / on rails.

I would like to do precisely this in ROR and I can get to the second  
last line of the above blog (that is I can generate the document and  
even the toByteArray or byte[] ). As in the example:

```
document.close // <<<--- I can get this document
send_data String.from_java_bytes(m.toByteArray),
     :type=> "application/pdf", :dispostion=>"inline"

```

// I can call the toByteArray and get the array of bytes byte[]

but I dont know how to send\_data to the browser with the document and  
byetarraystream available.

I try to do a byte[].toString() and send that to send\_data but when I  
get byte[].toString() from the java classes I get an error :

No Class DefFoundError (org/bouncycastle/asn1/ASN1OctetString)

Where to go from here?

From the blog : Is there an equivalent to String.from\_java\_bytes in  
ruby?

Ather S. wrote:

> Is there a way of sending this pdf file asdasd.pdf to the browser ; Send  
> binary data to the user as a file download as follows :
> 
> send\_data(BINARY\_DATA,{:filename=\>“asasd.pdf”,:type=\>“application/pdf”})
> 
> send\_data at :  
> [ActionController::Streaming](http://api.rubyonrails.org/classes/ActionController/Streaming.html)
> 
> What is BINARY\_DATA below? is it document, m, pwf\_writer?
> 
> BTW I am still unable to load this jar file using rjb … you are  
> welcome to see here :  
> [http://rubyforge.org/forum/forum.php?thread\_id=26875&forum\_id=8190](http://rubyforge.org/forum/forum.php?thread_id=26875&forum_id=8190)
> 
> miv wrote:
> 
> > require ‘rjb’  
> > Rjb::load(‘iText-2.1.2u.jar’)  
> > filestream = Rjb::import(‘java.io.FileOutputStream’)  
> > pdf\_writer = Rjb::import(“com.lowagie.text.pdf.PdfWriter”)\>  
> > document = Rjb::import(“com.lowagie.text.Document”)  
> > m = filestream.new(“asdasd.pdf”)  
> > document= document.new()  
> > writer= pdf\_writer.getInstance(document,m)  
> > document.open()  
> > document.add(par.new(“Hello World!”, font.new(arial)))  
> > document.add(par.new(“Now I run on Rails!”, font.new(arial)))  
> > document.close  
> > m.close

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [July 15, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/java-bridge-itext-example-anyone/142263/19 "2008-07-15T18:14:21Z")

</div>

Ok following is a posting on blogger about using itext using ROR:

> **[iText with Ruby on Rails using Ruby Java Bridge](http://techpanacea.blogspot.com/2008/07/itext-with-ruby-on-rails-using-ruby.html)**

Is there a better blogging site. I cannot format the code properly on  
this one?

How do I format it nicer?

Ather S. wrote:

> You can use send\_data by passing it a bytearray!!! no need for  
> String.fromjavabytes…
