Justification problem with PDF generators

April 3rd, 2008

Unfortunatly there seems to be a bug with justification in the PDF::Writer library, which I described in detail in a bug report on the PDF::Writer site. On the mailing list this was confirmed as a known bug.

Because I needed a solution I decided to have a look at the Ruby port of the well known PHP library FPDF, which is called Ruby FPDF by Olivier Plathey.

Justification works fine there, but there is no solution built in the base FPDF library to have justification with formatted text (some words in a flowing text which are bold, italic, have other font sizes, etc.). But there is a nice “flowing block” module by Damon Kohler for the PHP version of FPDF, which solves exactly this problem.

So I decided to to a Ruby port by my own, which is what I did and what I hope that you find useful as well. Here is an example how to use it:

require 'flowing_block'
pdf = PDF.new
pdf.newFlowingBlock( 40, 6, 0, 'L')
pdf.SetFont('Helvetica', 'B', 8)
pdf.WriteFlowingBlock('Bonjour Martin!')
pdf.newFlowingBlock( 40, 6, 0, 'J')
pdf.SetFont('Helvetica', 'B', 16)
pdf.WriteFlowingBlock('Hello ')
pdf.SetFont('Helvetica', 'I', 8)
pdf.WriteFlowingBlock('World! ')
pdf.WriteFlowingBlock('This is a test of the flowing block script.')
pdf.SetTextColor(255, 0, 0);  # red color.. ;-)
pdf.SetFont('Helvetica', 'B', 12)
pdf.WriteFlowingBlock('Rot! ')
pdf.SetTextColor(0, 255, 0);  # green color.. ;-)
pdf.WriteFlowingBlock(' of this should be justified correclty. It is sooooo amazing!! It is sooooo amazing!! It is sooooo amazing!! ')

I have put the Ruby FPDF library and the flowing block extension in ZIP-File which can be downloaded here.

Please have in mind that I am a Ruby beginner and that it’s very likely that there are lots of bugs in it. It’s also a matter of fact that the port is pretty ugly, mainly because I did not know a Ruby equivalent for the PHP pointer like =& operator. (If somebody knows how one can do that in Ruby, please post a comment!).

So if you find a bug or a solution how to improve the code please do so – I will gladly post the update here!

Riding on Rails…

April 3rd, 2008

I am new to Ruby and Ruby on Rails in particular and found out that it’s often easier the find answers to the complicated questions that to the easy ones. So I decided to start a blog about my ups and downs I am experiencing riding on Rails..

May the force of the chunky bacon be with this blog!