PDFs in Pure Ruby
prawn.majesticseacreature.com
prawn.majesticseacreature.com
Incidentally, although this use is quite off-label: Prawn exposes a very nice API for drawing arbitrary geometrical objects. ImageMagick/GraphicsMagick can convert PDF files to image files in your favorite format. This means you can use prawn to draw, e.g., an org chart for about a fiftieth of the pain it would take to draw it using graphics primitives in ImageMagick or what have you.
That reminds me -- I tried to throw money at the developers and they requested I send it to Heifer International. Somebody is getting a cow because I didn't have one writing my print jobs.
And also, thanks for donating!
[edit: fix github project reference]
require 'rubygems' require 'sinatra' require 'prawn'
get '/' do "<h1>This is a simple pdf rendering webservice. GET <a href='/hello%20world.pdf'>/hello%20world.pdf</a> to get a pdf back!</h1>" end
get '/*' do content_type('application/pdf') headers['Cache-Control'] = "public, max-age=6000000000000000000"
pdf = Prawn::Document.new
pdf.text(params[:splat][0].gsub(/.pdf\z/,''))
pdf.render
end(depends upon your needs, of course)
Only, I needed to parse plain HTML (not just XHTML) so I'm using TagSoup as the replacement parser: http://home.ccil.org/~cowan/XML/tagsoup/
This combination is awesome.
In order for this to happen, what we really need is a solid box model. Since we don't have that yet, it's a very hard problem. If someone wanted to pay me to work on it full time for a year, I could get it built.
But failing that, we'll have to wait a good deal of time for this, unless someone else finds a large chunk of dedicated time for it.
Until then, Flying Saucer is the way to go if you can't afford PrinceXML price tag.
iText is almost certainly significantly faster, but I haven't benchmarked. If there are very cool low level features in iText that you'd like to see in Prawn, let us know on our mailing list.
wc -l print_job.rb #1 file, 125 lines
This is not, strictly speaking, a fair comparison. In addition to being a superior version of everything the Java version does, the Ruby version also does GIF export, caching on the web server, and basic feature-wide analytics. The Java just barely staggers its way to generating a NxM grid and putting pleasingly aligned text in it. It is also littered with comments like:
// DO NOT EDIT. WILL NOT BE ABLE TO FIX. See r47 for what happened last time. (See r53.) (See r128.)
Do it in Prawn. I seriously have attempted to get my company to run a one-controller Rails app just to do PDF generation, because it would be cheaper going forward than the prodigious amount of effort we spend on report generation in Java. (This was, sadly, not approved.)
I did propose that to work, though (we do generation on the server so the size doesn't matter), and we have done it if we had an acceptable supply of Ruby-speaking engineers. Bad news for them, good news for me: I would hate to have their business hobbled when I leave.
It looks like it would be pretty difficult to add -- prawn is implemented as a clever DSL for writing postscript.
And actually, we have support for reading arbitrary PDF on edge, expect a release in a few weeks :)
# source, wikis, bug tracker http://github.com/sandal/prawn
# mailing list http://groups.google.com/group/prawn-ruby
# my twitter account (I announce all Prawn releases and other stuff there -- mixed in with personal stuff)
Just let us know what sort of feature set you want to implement, and we can point you to the relevant spec sections and supporting materials.
Prawn's source is pretty readable, and will be even more so as we refactor. A trip through the source might help, too:
The difference is the toolchain. All you need to do to run Prawn is dump the files into your project.