class PrawnPDFImage
Implements support for pulling an image out of an existing PDF and adds that image to a prawn document.
In this file, the pdf_* prefix refers to the PDF that contains the image we are adding to our document, prawn_* refers to the objects we are creating to be added to our prawn document.
Attributes
document[RW]
image_blob[RW]
reader[RW]
scaled_height[RW]
These methods are to conform to the API expected by Prawn Images and are not used internally.
scaled_width[RW]
These methods are to conform to the API expected by Prawn Images and are not used internally.
Public Class Methods
can_render?(image_blob)
click to toggle source
# File lib/prawn-pdfimage.rb, line 11 def self.can_render?(image_blob) image_blob.unpack('C5') == [37, 80, 68, 70, 45] end
new(image_blob)
click to toggle source
# File lib/prawn-pdfimage.rb, line 15 def initialize(image_blob) self.image_blob = image_blob self.reader = ::PDF::Reader.new StringIO.new(image_blob) end
Public Instance Methods
build_pdf_object(document)
click to toggle source
# File lib/prawn-pdfimage.rb, line 20 def build_pdf_object(document) self.document = document prawn_image_reference end
Private Instance Methods
page()
click to toggle source
# File lib/prawn-pdfimage.rb, line 110 def page reader.page 1 end
pdf_image_bits_per_component()
click to toggle source
# File lib/prawn-pdfimage.rb, line 94 def pdf_image_bits_per_component pdf_image_stream.hash[:BitsPerComponent] end
pdf_image_colorspace()
click to toggle source
In my tests all the PDF color spaces where contained in an ICCBased PDF reference to a dictionary with an Alternate key.
If images are not displaying correctly this is the first place to start looking.
# File lib/prawn-pdfimage.rb, line 80 def pdf_image_colorspace cs = pdf_image_stream.hash[:ColorSpace] if cs.is_a? Symbol cs else return cs[1].hash[:Alternate] end end
pdf_image_data()
click to toggle source
# File lib/prawn-pdfimage.rb, line 90 def pdf_image_data pdf_image_stream.unfiltered_data end
pdf_image_filter()
click to toggle source
# File lib/prawn-pdfimage.rb, line 71 def pdf_image_filter pdf_image_stream.hash[:Filter] end
pdf_image_height()
click to toggle source
# File lib/prawn-pdfimage.rb, line 98 def pdf_image_height pdf_image_stream.hash[:Height] end
Also aliased as: height
pdf_image_stream()
click to toggle source
# File lib/prawn-pdfimage.rb, line 106 def pdf_image_stream page.xobjects.first[1] end
pdf_image_width()
click to toggle source
# File lib/prawn-pdfimage.rb, line 102 def pdf_image_width pdf_image_stream.hash[:Width] end
Also aliased as: width
pdf_soft_mask()
click to toggle source
# File lib/prawn-pdfimage.rb, line 67 def pdf_soft_mask pdf_image_stream.hash[:SMask] end
prawn_image_reference()
click to toggle source
# File lib/prawn-pdfimage.rb, line 30 def prawn_image_reference document.ref!(prawn_image_reference_options).tap do |ref| ref << pdf_image_data ref.stream.filters << { pdf_image_filter => nil } ref.data[:SMask] = prawn_soft_mask if pdf_soft_mask end end
prawn_image_reference_options()
click to toggle source
# File lib/prawn-pdfimage.rb, line 38 def prawn_image_reference_options { Type: :XObject, Subtype: :Image, ColorSpace: pdf_image_colorspace, Height: pdf_image_height, Width: pdf_image_width, BitsPerComponent: pdf_image_bits_per_component } end
prawn_soft_mask()
click to toggle source
# File lib/prawn-pdfimage.rb, line 49 def prawn_soft_mask document.ref!(prawn_soft_mask_options).tap do |ref| ref.stream << pdf_soft_mask.unfiltered_data ref.stream.filters << { FlateDecode: nil } end end
prawn_soft_mask_options()
click to toggle source
# File lib/prawn-pdfimage.rb, line 56 def prawn_soft_mask_options { Type: :XObject, Subtype: :Image, Width: pdf_image_width, Height: pdf_image_height, BitsPerComponent: pdf_image_bits_per_component, ColorSpace: :DeviceGray } end