Java Bridge Itext Example Anyone?

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/

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?

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/

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?

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 White 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

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 :slight_smile:

Craig White 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")

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

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:

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:

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')
>

Craig White wrote:

Craig White 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.

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?

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:

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 :
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

miv wrote:

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

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 Shiraz wrote:

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

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?

Craig White wrote:

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

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 Shiraz wrote: