Ruby on Rails
ErrorUsingFPDFWithJPGOrPNGOnMacOS

When using FPDF to generate PDF out of Ruby, we’re constantly getting errors in the FPDF library when we try to include JPG files or PNG files in the PDF output.
Rails 0.13.1, Mac OS 10.4.2

The error is for JPG inclusion:


<span class="newWikiWord">RuntimeError<a href="http://wiki.rubyonrails.com/rails/pages/RuntimeError">?</a></span> in Veranstaltungs#pdf

Missing or incorrect JPEG file: /somepathtoproject/public/images/print-header.jpg
/lib/fpdf/fpdf.rb:1302:in `parsejpg'
/lib/fpdf/fpdf.rb:821:in `Image'
/lib/vvpdf/vvpdf.rb:41:in `Header'
/lib/fpdf/fpdf.rb:309:in `AddPage'
/app/controllers/veranstaltungs_controller.rb:229:in `gen_pdf_module'
/app/controllers/veranstaltungs_controller.rb:160:in `gen_pdf'
/app/controllers/veranstaltungs_controller.rb:150:in `pdf'
script/server:49

whereas the error for PNG inclusion is a little bit different:


<a href="http://wiki.rubyonrails.com/rails/pages/NoMethodError" class="existingWikiWord">NoMethodError</a> in Veranstaltungs#pdf

undefined method `unpack' for nil:NilClass
/lib/fpdf/fpdf.rb:1404:in `freadint'
/lib/fpdf/fpdf.rb:1366:in `parsepng'
/lib/fpdf/fpdf.rb:823:in `Image'
/lib/vvpdf/vvpdf.rb:41:in `Header'
/lib/fpdf/fpdf.rb:309:in `AddPage'
/app/controllers/veranstaltungs_controller.rb:229:in `gen_pdf_module'
/app/controllers/veranstaltungs_controller.rb:160:in `gen_pdf'
/app/controllers/veranstaltungs_controller.rb:150:in `pdf'
script/server:49

Has anyone experienced similar problems?

Yes, with PDF::Writer—and it’s a MacOS X Tiger problem. See the main PDF generation page for the solution. It’s generally getting a proper build of Ruby instead of the bad one. Also note that the description of the platform at the top was incorrect—the person who reported this originally was not using “Ruby 0.13.1”; they were using “Rails 0.13.1”. Please ensure that you know the difference in the future. —Austin

Maybe the image path isn’t correct? I had the same problem and got it working using paths like this:
RAILS_ROOT + '/public/images/print-header.jpg'

category: OpenQuestion