Module: HexaPDF::ImageLoader::PDF

Defined in:
lib/hexapdf/image_loader/pdf.rb

Overview

This module is used for loading the first page of a PDF file.

Loaded PDF graphics are represented by form XObjects instead of image XObjects. However, the image/xobject drawing methods of HexaPDF::Content::Canvas know how to handle them correctly so that this doesn’t matter from a user’s point of view.

See: PDF2.0 s8.10

Constant Summary collapse

MAGIC_FILE_MARKER =

The magic marker that tells us if the file/IO contains an PDF file.

"%PDF-".b

Class Method Summary collapse

Class Method Details

.handles?(file_or_io) ⇒ Boolean

:call-seq:

PDF.handles?(filename)     -> true or false
PDF.handles?(io)           -> true or false

Returns true if the given file or IO stream can be handled, ie. if it contains an image in JPEG format.

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/hexapdf/image_loader/pdf.rb', line 61

def self.handles?(file_or_io)
  if file_or_io.kind_of?(String)
    File.read(file_or_io, 5, mode: 'rb') == MAGIC_FILE_MARKER
  else
    file_or_io.rewind
    file_or_io.read(5) == MAGIC_FILE_MARKER
  end
end

.load(document, file_or_io) ⇒ Object

:call-seq:

PDF.load(document, filename)    -> form_obj
PDF.load(document, io)          -> form_obj

Creates a PDF form XObject from the PDF file or IO stream.

See: DefaultConfiguration for the meaning of ‘image_loader.pdf.use_stringio’.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hexapdf/image_loader/pdf.rb', line 77

def self.load(document, file_or_io)
  idoc = if file_or_io.kind_of?(String) && document.config['image_loader.pdf.use_stringio']
           HexaPDF::Document.open(file_or_io)
         elsif file_or_io.kind_of?(String)
           HexaPDF::Document.new(io: File.open(file_or_io, 'rb'))
         else
           HexaPDF::Document.new(io: file_or_io)
         end
  form = idoc.pages[0].to_form_xobject
  document.add(document.import(form))
end