Class: Mindee::PDF::ExtractedPDF

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/pdf/extracted_pdf.rb

Overview

An extracted sub-Pdf.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf_stream, filename) ⇒ ExtractedPDF

Returns a new instance of ExtractedPDF.

Parameters:

  • pdf_stream (StringIO, File)
  • filename (String)


18
19
20
21
22
23
24
25
26
27
# File 'lib/mindee/pdf/extracted_pdf.rb', line 18

def initialize(pdf_stream, filename)
  @filename = filename

  if pdf_stream.is_a?(File)
    pdf_stream.rewind
    @pdf_bytes = StringIO.new(pdf_stream.read)
  else
    @pdf_bytes = pdf_stream
  end
end

Instance Attribute Details

#filenameString (readonly)

Name of the file.

Returns:

  • (String)


14
15
16
# File 'lib/mindee/pdf/extracted_pdf.rb', line 14

def filename
  @filename
end

#pdf_bytesStringIO (readonly)

Byte contents of the pdf

Returns:

  • (StringIO)


10
11
12
# File 'lib/mindee/pdf/extracted_pdf.rb', line 10

def pdf_bytes
  @pdf_bytes
end

Instance Method Details

#as_input_sourceMindee::Input::Source::BytesInputSource

Returns the current PDF object as a usable BytesInputSource.



59
60
61
62
63
64
65
66
67
# File 'lib/mindee/pdf/extracted_pdf.rb', line 59

def as_input_source
  raise Error::MindeePDFError, 'Bytes object is nil.' if @pdf_bytes.nil?

  @pdf_bytes.rewind if @pdf_bytes.respond_to?(:rewind)
  data = @pdf_bytes.read || ''
  @pdf_bytes.rewind if @pdf_bytes.respond_to?(:rewind)

  Mindee::Input::Source::BytesInputSource.new(data, @filename)
end

#page_countInteger

Retrieves the page count for a given pdf.

Returns:

  • (Integer)


31
32
33
34
35
36
# File 'lib/mindee/pdf/extracted_pdf.rb', line 31

def page_count
  current_pdf = Mindee::PDF::PDFProcessor.open_pdf(pdf_bytes)
  current_pdf.pages.size
rescue TypeError, Origami::InvalidPDFError
  raise Error::MindeePDFError, 'Could not retrieve page count from Extracted PDF object.'
end

#write_to_file(output_path, override: false) ⇒ Object

Writes the contents of the current PDF object to a file.

Parameters:

  • output_path (String)

    Path to write to.

  • override (bool) (defaults to: false)

    Whether to override the destination file.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mindee/pdf/extracted_pdf.rb', line 41

def write_to_file(output_path, override: false)
  raise Error::MindeePDFError, 'Provided path is not a file' if File.directory?(output_path)
  raise Error::MindeePDFError, 'Invalid save path provided' unless File.exist?(
    File.expand_path('..', output_path)
  ) && !override

  if File.extname(output_path).downcase == 'pdf'
    base_path = File.expand_path('..', output_path)
    output_path = File.expand_path("#{File.basename(output_path)}.pdf", base_path)
  end

  @pdf_bytes.rewind if @pdf_bytes.respond_to?(:rewind)
  File.binwrite(output_path, @pdf_bytes.read.to_s)
  @pdf_bytes.rewind if @pdf_bytes.respond_to?(:rewind)
end