Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Mar 05

Pdf writer gem: history of a patch

Posted by Francesca Resta in Ruby on Rails - comments are closed digg this add to delicious

Lately I was using this wonderful plugin to create PDF documents with ruby (version 1.1.3). It did its job quite well for what I needed it to, but when I was asked to hide the acrobat toolbar in the viewer, I got some problems. Actually, the plugin’s class “ViewerPreferences” was exactly what I was looking for, but if I use it, it throw me an error while opening the generated pdf file.

The code that print that part of the pdf is:

# File lib/pdf/writer/object/viewerpreferences.rb
66:    def to_s
67:        res = "\n#{@id} 0 obj\n<< " 
68:        Preferences.each do |s|
69:            v = __send__("#{s.downcase}".intern)
70:            res << "\n/#{s} /#{v}" unless v.nil?
71:        end
72:        res << "\n>>\n" 
73:    end

Comparing the generated file with a working one, I fixed that function this way:

66:    def to_s
67:        res = "\n#{@oid} 0 obj\n<< " 
68:        Preferences.each do |s|
69:            v = __send__("#{s.downcase}".intern)
70:            res << "\n/#{s} #{v}" unless v.nil?
71:        end
72:        res << "\n>>\nendobj\n" 
73:    end

There also a patch file avaiable here

I’m not very into pdf coding, so perhaps there was a more stylish way to write it, or I done some mistakes, so I will be very happy about every suggestion to improve the code.

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory