//
you're reading...
PDF

How to insert a jpeg image into pdf with rfpdf and Image Science

I was using a 2006 version of rfpdf with tcpdf support (I don’t remember where I got it). It was the only working version I was able to find.

Once I tried to insert images into a pdf document, I discovered that it doesn’t work with jpg. The function GetImageSize was called but didn’t exist.

I searched the internet and it’d been already fixed. The problem was that those couple of versions of rfpdf either didn’t work or didn’t exist in the repository.

The solutions were here
and here (specific for ImageScience).

As it translated to my own code, I tuned it for ImageScience and fixed all typos and bugs in the code:

1. Add the following lines to the plugin rfpdf/environment.rb

require 'image_science'
require "#{File.dirname __FILE__}/lib/core/image_science"

2. Add file rfpdf/lib/core/image_science.rb

module RFPDF

  # http://uk2.php.net/getimagesize
  def getimagesize(filename)
    out = Hash.new
    
    ImageScience.with_image(filename) do |img|
      out[0] = img.width
      out[1] = img.height
      out[2] = img.instance_values["file_type"]
      out[3] = "height=\"#{img.height}\" width=\"#{img.width}\""
    end
          
    out
  end
  
end

3. Fix typos in rfpdf/lib/tcpdf.rb

Advertisements

About RailsBlogger

I'm a Software Developer with over 10 years of experience, Java and Ruby on Rails.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: