Generating PDF Documents with Rails and PDFlib - Part I

Posted by Bob Silva Sun, 02 Apr 2006 14:16:00 GMT


Generating PDF documents programmatically is probably one of the worse programming tasks I've had the pleasure to work on. It's not terribly difficult, it's just tedious and time consuming. Some of my applications need to print catalogs or reports upwards of 300+ pages in real-time, so speed is of the essence, unfortunately, the pure Ruby PDF::Writer isn't up to the job. PDFlib-Lite, (read the license before using), is a fast library, which in the lastest 6.0.3 release contains Ruby Bindings (albeit horribly broken).

Installing PDFlib-Lite


Installing PDFlib is similar on Linux and MacOSX.

Download the lastest tarball

Extract the source

src> tar zxf PDFlib-Lite-6.0.3.tar.gz
src> cd PDFlib-Lite-6.0.3


If using 6.0.3, download this patch to the root of your extracted PDFlib-Lite source, apply the patch, and regenerate the configure script

PDFlib-Lite-6.0.3> patch -p0 < configure.in.diff
PDFlib-Lite-6.0.3> autoconf


Run the configure script. Use --help for additional options/language bindings. Your paths may be different, if you can't figure it out, switch to Windows.

On linux:
PDFlib-Lite-6.0.3> ./configure --with-ruby=/usr/bin/ruby --with-rubyincl=/usr/lib/ruby/1.8/i686-linux


On MacOSX (you built your own Ruby right?):
PDFlib-Lite-6.0.3> ./configure --with-ruby=/usr/local/bin/ruby --with-rubyincl=/usr/local/lib/ruby/1.8/i686-darwin8.5.2


If you followed these instructions, the configure script should tell you that the Ruby bindings are active.

Ruby language binding for PDFlib: yes


Build the library, (the 'make test' is required for these instructions to work)

PDFlib-Lite-6.0.3> make
PDFlib-Lite-6.0.3> make test
PDFlib-Lite-6.0.3> make install


All right, so now you'd think it would work since you've installed it. Remember when I said the Ruby bindings were broken, besides the patch to get PDFlib to realize it has Ruby bindings, you also have to manually copy the library to your Ruby's site_ruby/ directory. Optionally, you can symlink the real library found in your /usr/lib or /usr/local/lib directory.

PDFlib-Lite-6.0.3> cd bind/pdflib/ruby

On linux:
PDFlib-Lite-6.0.3/bind/pdflib/ruby> cp PDFlib.so /usr/lib/ruby/site_ruby/1.8


On MacOSX:
PDFlib-Lite-6.0.3/bind/pdflib/ruby> cp PDFlib.bundle /usr/local/lib/ruby/site_ruby/1.8



We'll cover usage of PDFlib in part II of this article which should be complete in a week or so.

Comments

Leave a response

Comments