Module: Prawn::Images

Included in:
Document
Defined in:
lib/prawn/images.rb,
lib/prawn/images/jpg.rb,
lib/prawn/images/png.rb

Defined Under Namespace

Classes: JPG, PNG

Instance Method Summary collapse

Instance Method Details

#image(filename, options = {}) ⇒ Object

add the image at filename to the current page. Currently only JPG and PNG files are supported.

Arguments:

filename

the path to the file to be embedded

Options:

:at

the location of the top left corner of the image.

<tt>:position/tt>
:height

the height of the image [actual height of the image]

:width

the width of the image [actual width of the image]

:scale

scale the dimensions of the image proportionally

Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do     
  pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg" 
  image pigs, :at => [50,450], :width => 450                                      

  dice = "#{Prawn::BASEDIR}/data/images/dice.png"
  image dice, :at => [50, 450], :scale => 0.75 
end

If only one of :width / :height are provided, the image will be scaled proportionally. When both are provided, the image will be stretched to fit the dimensions without maintaining the aspect ratio.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prawn/images.rb', line 39

def image(filename, options={})
  raise ArgumentError, "#{filename} not found" unless File.file?(filename)  
  
  
  read_mode = ruby_18 { "rb" } || ruby_19 { "rb:ASCII-8BIT" }
  image_content =  File.open(filename, read_mode) { |f| f.read }
  
  image_sha1 = Digest::SHA1.hexdigest(image_content)

  # register the fact that the current page uses images
  proc_set :ImageC

  # if this image has already been embedded, just reuse it
  image_obj = image_registry[image_sha1]

  if image_registry[image_sha1]
    info = image_registry[image_sha1][:info]
    image_obj = image_registry[image_sha1][:obj]
  else
    # build the image object and embed the raw data
    image_obj = case detect_image_format(image_content)
    when :jpg then
      info = Prawn::Images::JPG.new(image_content)
      build_jpg_object(image_content, info)
    when :png then
      info = Prawn::Images::PNG.new(image_content)
      build_png_object(image_content, info)
    end
    image_registry[image_sha1] = {:obj => image_obj, :info => info}
  end

  # find where the image will be placed and how big it will be  
  w,h = calc_image_dimensions(info, options)
  if options[:at]       
    x,y = translate(options[:at]) 
  else                  
    x,y = image_position(w,h,options) 
    move_text_position h   
  end

  # add a reference to the image object to the current page
  # resource list and give it a label
  label = "I#{next_image_id}"
  page_xobjects.merge!( label => image_obj )

  # add the image to the current page
  instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
  add_content instruct % [ w, h, x, y - h, label ]
end