Class: Mindee::PDF::PDFExtractor::ExtractedPDF
- Inherits:
-
Object
- Object
- Mindee::PDF::PDFExtractor::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 ⇒ StreamIO
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_bytes, 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_bytes, filename) ⇒ ExtractedPDF
Returns a new instance of ExtractedPDF.
19 20 21 22 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 19 def initialize(pdf_bytes, filename) @pdf_bytes = pdf_bytes @filename = filename end |
Instance Attribute Details
#filename ⇒ String (readonly)
Name of the file.
15 16 17 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 15 def filename @filename end |
#pdf_bytes ⇒ StreamIO (readonly)
Byte contents of the pdf
11 12 13 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 11 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.
52 53 54 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 52 def as_input_source Mindee::Input::Source::BytesInputSource.new(@pdf_bytes.read, @filename) end |
#page_count ⇒ Integer
Retrieves the page count for a given pdf.
26 27 28 29 30 31 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 26 def page_count current_pdf = Mindee::PDF::PDFProcessor.open_pdf(pdf_bytes) current_pdf.pages.size rescue TypeError, Origami::InvalidPDFError raise Errors::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.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mindee/pdf/extracted_pdf.rb', line 36 def write_to_file(output_path, override: false) raise Errors::MindeePDFError, 'Provided path is not a file' if File.directory?(output_path) raise Errors::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 File.write(output_path, @pdf_bytes) end |