Sha256: 863c57de21e9ae70b5a373b66ffbb1f481b21feffc6947488c1fd33a89fdb88e

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# coding: utf-8

require 'tempfile'
require 'base64'

module ThinReports
  module Generator
    
    module Pdf::Graphics
      # @param [String] filename
      # @param [Numeric, Strng] x
      # @param [Numeric, Strng] y
      # @param [Numeric, Strng] w
      # @param [Numeric, Strng] h
      def image(filename, x, y, w, h)
        w, h = s2f(w, h)
        pdf.image(filename, :at => pos(x, y), :width  => w, :height => h)
      end
      
      # @param [String] base64
      # @param x, y, w, h (see #image)
      def base64image(base64, x, y, w, h)
        image = create_temp_imagefile(base64)
        image(image.path, x, y, w, h)
      end
      
    private
      
      def clean_temp_images
        temp_image_registry.each {|tmp| tmp.unlink }
      end
      
      def temp_image_registry
        @temp_image_registry ||= []
      end
      
      # @param [String] base64
      # @return [Tempfile]
      def create_temp_imagefile(base64)
        file = Tempfile.new('trg-tmp-img')
        file.binmode
        file.write(Base64.decode64(base64))
        temp_image_registry << file
        file
      ensure
        file.close
      end      
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thinreports-0.6.0.pre3 lib/thinreports/generator/pdf/document/graphics/image.rb