Method: PDF::Writer::Graphics#image
- Defined in:
- lib/extensions/pdf-writer/pdf/writer/graphics.rb
#image(image, options = {}) ⇒ Object
Add an image easily to a PDF document. image
is the name of a JPG or PNG image. options
is a Hash:
:pad
-
The number of PDF userspace units that will be on all sides of the image. The default is
5
units. :width
-
The desired width of the image. The image will be resized to this width with the aspect ratio kept. If unspecified, the image’s natural width will be used.
:resize
-
How to resize the image, either :width (resizes the image to be as wide as the margins) or :full (resizes the image to be as large as possible). May be a numeric value, used as a multiplier for the image size (e.g., 0.5 will shrink the image to half-sized). If this and
:width
are unspecified, the image’s natural size will be used. Mutually exclusive with the <tt>:width<tt> option. :justification
-
The placement of the image. May be :center, :right, or :left. Defaults to :left.
:border
-
The border options. No default border. If specified, must be either
true
, which uses the default border, or a Hash. :link
-
Makes the image a clickable link.
Image borders are specified as a hash with two options:
:color
-
The colour of the border. Defaults to 50% grey.
:style
-
The stroke style of the border. This must be a StrokeStyle object and defaults to the default line.
Image links are defined as a hash with two options:
: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.
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/extensions/pdf-writer/pdf/writer/graphics.rb', line 648 def image(image, = {}) width = [:width] pad = [:pad] || 5 resize = [:resize] just = [:justification] || :left border = [:border] link = [:link] if image.kind_of?(PDF::Writer::External::Image) info = image.image_info image_data = image else if image.respond_to?(:read) image_data = image.read else image_data = open(image, "rb") { |file| file.read } end info = PDF::Writer::Graphics::ImageInfo.new(image_data) end raise "Unsupported Image Type" unless %w(JPEG PNG).include?(info.format) width = info.width if width.nil? aspect = info.width.to_f / info.height.to_f # Get the maximum width of the image on insertion. if @columns_on max_width = @columns[:width] - (pad * 2) else max_width = @page_width - (pad * 2) - @left_margin - @right_margin end if resize == :full or resize == :width or width > max_width width = max_width end # Keep the height in an appropriate aspect ratio of the width. height = (width / aspect.to_f) # Resize the image. if resize.kind_of?(Numeric) width *= resize height *= resize end # Resize the image *again*, if it is wider than what is available. if width > max_width height = (width / aspect.to_f) end # If the height is greater than the available space: havail = @y - @bottom_margin - (pad * 2) if height > havail # If the image is to be resized to :full (remaining space # available), adjust the image size appropriately. Otherwise, start # a new page and flow to the next page. if resize == :full height = havail width = (height * aspect) else start_new_page end end # Find the x and y positions. y = @y - pad - height x = @left_margin + pad if (width < max_width) case just when :center x += (max_width - width) / 2.0 when :right x += (max_width - width) end end image_obj = add_image(image_data, x, y, width, height, info) if border border = {} if true == border border[:color] ||= Color::RGB::Grey50 border[:style] ||= PDF::Writer::StrokeStyle::DEFAULT save_state stroke_color border[:color] stroke_style border[:style] rectangle(x, y - pad, width, height - pad).stroke restore_state end if link case link[:type] when :internal add_internal_link(link[:target], x, y - pad, x + width, y + height - pad) when :external add_link(link[:target], x, y - pad, x + width, y + height - pad) end end @y = @y - pad - height image_obj end |