Sha256: c4a3424c952b3a819b338dcd32fb8e9c33ec66b722e6ca7d03fdcb9a7850d143

Contents?: true

Size: 1.61 KB

Versions: 11

Compression:

Stored size: 1.61 KB

Contents

require "rabbit/utils"

Rabbit::Utils.require_safe "poppler"

require "rabbit/image/base"

module Rabbit
  module ImageManipulable
    class PDF < Base

      unshift_loader(self)

      class << self
        def match?(filename)
          File.open(filename) do |f|
            line = f.gets
            line and /\A%PDF-1\.\d\z/ =~ line.chomp
          end
        rescue ArgumentError
          false
        end
      end

      def draw(canvas, x, y, params={})
        if @doc and canvas.poppler_available?
          default_params = {
            :width => width,
            :height => height,
          }
          canvas.draw_poppler_page(page, x, y, default_params.merge(params))
        else
          super
        end
      end

      def pixbuf
        @pixbuf ||= to_pixbuf
      end

      private
      def page
        index = self["page"] || 0
        begin
          index = Integer(index)
        rescue ArgumentError
        end
        _page = @doc[index]
        if _page.nil?
          message = _("%s page isn't exist in PDF") % index.inspect
          raise ImageLoadError.new("#{@filename}: #{message}")
        end
        _page
      end

      def update_size
        @doc = Poppler::Document.new(uri)
        @width, @height = page.size
      end

      def filename
        File.expand_path(@filename)
      end

      def uri
        "file://#{filename}"
      end

      def to_pixbuf
        w = original_width
        h = original_height
        pixbuf = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB, true, 8, w, h)
        page.render(0, 0, w, h, 1.0, 0, pixbuf)
        pixbuf
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rabbit-2.0.5 lib/rabbit/image/pdf.rb
rabbit-2.0.4 lib/rabbit/image/pdf.rb
rabbit-2.0.3 lib/rabbit/image/pdf.rb
rabbit-2.0.2 lib/rabbit/image/pdf.rb
rabbit-2.0.1 lib/rabbit/image/pdf.rb
rabbit-2.0.0 lib/rabbit/image/pdf.rb
rabbit-1.0.9 lib/rabbit/image/pdf.rb
rabbit-1.0.8 lib/rabbit/image/pdf.rb
rabbit-1.0.7 lib/rabbit/image/pdf.rb
rabbit-1.0.6 lib/rabbit/image/pdf.rb
rabbit-1.0.5 lib/rabbit/image/pdf.rb