Class: Mindee::PDF::ExtractedPDF
- Inherits:
-
Object
- Object
- Mindee::PDF::ExtractedPDF
- Defined in:
- lib/mindee/pdf/extracted_pdf.rb
Overview
An extracted sub-Pdf.
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
Name of the file.
-
#pdf_bytes ⇒ StringIO
readonly
Byte contents of the pdf.
Instance Method Summary collapse
-
#as_input_source ⇒ Mindee::Input::Source::BytesInputSource
Returns the current PDF object as a usable BytesInputSource.
-
#initialize(pdf_stream, filename) ⇒ ExtractedPDF
constructor
A new instance of ExtractedPDF.
-
#page_count ⇒ Integer
Retrieves the page count for a given pdf.
-
#write_to_file(output_path, override: false) ⇒ Object
Writes the contents of the current PDF object to a file.
Constructor Details
#initialize(pdf_stream, filename) ⇒ ExtractedPDF
Returns a new instance of ExtractedPDF.
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
#filename ⇒ String (readonly)
Name of the file.
14 15 16 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 14 def filename @filename end |
#pdf_bytes ⇒ StringIO (readonly)
Byte contents of the pdf
10 11 12 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 10 def pdf_bytes @pdf_bytes end |
Instance Method Details
#as_input_source ⇒ Mindee::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_count ⇒ Integer
Retrieves the page count for a given pdf.
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.
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.('..', output_path) ) && !override if File.extname(output_path).downcase == 'pdf' base_path = File.('..', output_path) output_path = File.("#{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 |