Method: PDF::Writer::Graphics#add_image

Defined in:
lib/pdf/writer/graphics.rb

#add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil) ⇒ Object

Add an image from a loaded image (JPEG or PNG) resource at position (x, y) (the lower left-hand corner of the image) and scaled to width by height units. If provided, image_info is a PDF::Writer::Graphics::ImageInfo object.

In PDF::Writer 1.1 or later, the new link parameter is a hash with two keys:

:type

The type of link, either :internal or :external.

:target

The destination of the link. For an :internal link, this is an internal cross-reference destination. For an :external link, this is an URI.

This will automatically make the image a clickable link if set.



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/pdf/writer/graphics.rb', line 568

def add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil)
  if image.kind_of?(PDF::Writer::External::Image)
    label       = image.label
    image_obj   = image
    image_info ||= image.image_info
  else
    image_info ||= PDF::Writer::Graphics::ImageInfo.new(image)

    tt = Time.now
    @images << tt
    id = @images.index(tt)
    label = "I#{id}"
    image_obj = PDF::Writer::External::Image.new(self, image, image_info, label)
    @images[id] = image_obj
  end

  if width.nil? and height.nil?
    width   = image_info.width
    height  = image_info.height
  end

  width  ||= height / image_info.height.to_f * image_info.width
  height ||= width * image_info.height / image_info.width.to_f

  tt = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
  add_content(tt % [ width, height, x, y, label ])

  if link
    case link[:type]
    when :internal
      add_internal_link(link[:target], x, y, x + width, y + height)
    when :external
      add_link(link[:target], x, y, x + width, y + height)
    end
  end

  image_obj
end