Method: HexaPDF::Document.open

Defined in:
lib/hexapdf/document.rb

.open(filename, **kwargs) ⇒ Object

:call-seq:

Document.open(filename, **docargs)                   -> doc
Document.open(filename, **docargs) {|doc| block}     -> obj

Creates a new PDF Document object for the given file.

Depending on whether a block is provided, the functionality is different:

  • If no block is provided, the whole file is instantly read into memory and the PDF Document created for it is returned.

  • If a block is provided, the file is opened and a PDF Document is created for it. The created document is passed as an argument to the block and when the block returns the associated file object is closed. The value of the block will be returned.

The block version is useful, for example, when you are dealing with a large file and you only need a small portion of it.

The provided keyword arguments (except io) are passed on unchanged to Document.new.



95
96
97
98
99
100
101
102
103
# File 'lib/hexapdf/document.rb', line 95

def self.open(filename, **kwargs)
  if block_given?
    File.open(filename, 'rb') do |file|
      yield(new(**kwargs, io: file))
    end
  else
    new(**kwargs, io: StringIO.new(File.binread(filename)))
  end
end