module PDF
class Wrapper
# add an image to the page - a wide range of image formats are supported,
# including svg, jpg, png and gif. PDF images are also supported - an attempt
# to add a multipage PDF will result in only the first page appearing in the
# new document.
#
# supported options:
# :left:: The x co-ordinate of the left-hand side of the image.
# :top:: The y co-ordinate of the top of the image.
# :height:: The height of the image
# :width:: The width of the image
# :proportional:: Boolean. Maintain image proportions when scaling. Defaults to false.
# :padding:: Add some padding between the image and the specified box.
# :center:: If the image is scaled, it will be centered horizontally and vertically
# :rotate:: The desired rotation. One of :counterclockwise, :upsidedown, :clockwise.
# Doesn't work with PNG, PDF or SVG files.
#
# left and top default to the current cursor location
# width and height default to the size of the imported image
# padding defaults to 0
def image(filename, opts = {})
# TODO: add some options for justification and padding
raise ArgumentError, "file #{filename} not found" unless File.file?(filename)
opts.assert_valid_keys(default_positioning_options.keys + [:padding, :proportional, :center, :rotate])
if opts[:padding]
opts[:left] += opts[:padding].to_i if opts[:left]
opts[:top] += opts[:padding].to_i if opts[:top]
opts[:width] -= opts[:padding].to_i * 2 if opts[:width]
opts[:height] -= opts[:padding].to_i * 2 if opts[:height]
end
case detect_image_type(filename)
when :pdf then draw_pdf filename, opts
when :png then draw_png filename, opts
when :svg then draw_svg filename, opts
else
draw_pixbuf filename, opts
end
end
private
def detect_image_type(filename)
# read the first Kb from the file to attempt file type detection
f = File.new(filename)
bytes = f.read(1024)
# if the file is a PNG
if bytes[1,3].eql?("PNG")
return :png
elsif bytes[0,3].eql?("GIF")
return :gif
elsif bytes[0,4].eql?("%PDF")
return :pdf
elsif bytes.include?("