Class: PDF::Reader
- Inherits:
-
Object
- Object
- PDF::Reader
- Defined in:
- lib/pdf/reader.rb,
lib/pdf/reader/lzw.rb,
lib/pdf/reader/cmap.rb,
lib/pdf/reader/font.rb,
lib/pdf/reader/page.rb,
lib/pdf/reader/xref.rb,
lib/pdf/reader/error.rb,
lib/pdf/reader/point.rb,
lib/pdf/reader/token.rb,
lib/pdf/reader/buffer.rb,
lib/pdf/reader/filter.rb,
lib/pdf/reader/parser.rb,
lib/pdf/reader/stream.rb,
lib/pdf/reader/encoding.rb,
lib/pdf/reader/text_run.rb,
lib/pdf/reader/rectangle.rb,
lib/pdf/reader/reference.rb,
lib/pdf/reader/resources.rb,
lib/pdf/reader/cid_widths.rb,
lib/pdf/reader/filter/lzw.rb,
lib/pdf/reader/glyph_hash.rb,
lib/pdf/reader/page_state.rb,
lib/pdf/reader/type_check.rb,
lib/pdf/reader/filter/null.rb,
lib/pdf/reader/object_hash.rb,
lib/pdf/reader/page_layout.rb,
lib/pdf/reader/filter/flate.rb,
lib/pdf/reader/form_xobject.rb,
lib/pdf/reader/object_cache.rb,
lib/pdf/reader/object_stream.rb,
lib/pdf/reader/filter/ascii85.rb,
lib/pdf/reader/key_builder_v5.rb,
lib/pdf/reader/no_text_filter.rb,
lib/pdf/reader/pages_strategy.rb,
lib/pdf/reader/print_receiver.rb,
lib/pdf/reader/font_descriptor.rb,
lib/pdf/reader/filter/ascii_hex.rb,
lib/pdf/reader/filter/depredict.rb,
lib/pdf/reader/filter/run_length.rb,
lib/pdf/reader/register_receiver.rb,
lib/pdf/reader/page_text_receiver.rb,
lib/pdf/reader/synchronized_cache.rb,
lib/pdf/reader/validating_receiver.rb,
lib/pdf/reader/rc4_security_handler.rb,
lib/pdf/reader/standard_key_builder.rb,
lib/pdf/reader/null_security_handler.rb,
lib/pdf/reader/transformation_matrix.rb,
lib/pdf/reader/zero_width_runs_filter.rb,
lib/pdf/reader/aes_v2_security_handler.rb,
lib/pdf/reader/aes_v3_security_handler.rb,
lib/pdf/reader/overlapping_runs_filter.rb,
lib/pdf/reader/security_handler_factory.rb,
lib/pdf/reader/width_calculator/built_in.rb,
lib/pdf/reader/width_calculator/composite.rb,
lib/pdf/reader/width_calculator/true_type.rb,
lib/pdf/reader/width_calculator/type_zero.rb,
lib/pdf/reader/bounding_rectangle_runs_filter.rb,
lib/pdf/reader/unimplemented_security_handler.rb,
lib/pdf/reader/width_calculator/type_one_or_three.rb
Overview
typed: strict frozen_string_literal: true
Defined Under Namespace
Modules: Filter, WidthCalculator Classes: AesV2SecurityHandler, AesV3SecurityHandler, BoundingRectangleRunsFilter, Buffer, CMap, CidWidths, Encoding, EncryptedPDFError, Error, EventPoint, Font, FontDescriptor, FormXObject, GlyphHash, InvalidObjectError, InvalidPageError, KeyBuilderV5, LZW, MalformedPDFError, NoTextFilter, NullSecurityHandler, ObjectCache, ObjectHash, ObjectStream, OverlappingRunsFilter, Page, PageLayout, PageState, PageTextReceiver, PagesStrategy, Parser, Point, PrintReceiver, Rc4SecurityHandler, Rectangle, Reference, RegisterReceiver, Resources, SecurityHandlerFactory, StandardKeyBuilder, Stream, SynchronizedCache, TextRun, Token, TransformationMatrix, TypeCheck, UnimplementedSecurityHandler, UnsupportedFeatureError, ValidatingReceiver, XRef, ZeroWidthRunsFilter
Instance Attribute Summary collapse
-
#objects ⇒ Object
readonly
lowlevel hash-like access to all objects in the underlying PDF.
Class Method Summary collapse
-
.open(input, opts = {}) {|PDF::Reader.new(input, opts)| ... } ⇒ Object
syntactic sugar for opening a PDF file and the most common approach.
Instance Method Summary collapse
-
#info ⇒ Object
Return a Hash with some basic information about the PDF file.
-
#initialize(input, opts = {}) ⇒ Reader
constructor
creates a new document reader for the provided PDF.
-
#metadata ⇒ Object
Return a String with extra XML metadata provided by the author of the PDF file.
-
#page(num) ⇒ Object
returns a single PDF::Reader::Page for the specified page.
-
#page_count ⇒ Object
To number of pages in this PDF.
-
#pages ⇒ Object
returns an array of PDF::Reader::Page objects, one for each page in the source PDF.
-
#pdf_version ⇒ Object
The PDF version this file uses.
Constructor Details
#initialize(input, opts = {}) ⇒ Reader
creates a new document reader for the provided PDF.
input can be an IO-ish object (StringIO, File, etc) containing a PDF or a filename
reader = PDF::Reader.new("somefile.pdf")
File.open("somefile.pdf","rb") do |file|
reader = PDF::Reader.new(file)
end
If the source file is encrypted you can provide a password for decrypting
reader = PDF::Reader.new("somefile.pdf", :password => "apples")
Using this method directly is supported, but it’s more common to use ‘PDF::Reader.open`
118 119 120 121 122 |
# File 'lib/pdf/reader.rb', line 118 def initialize(input, opts = {}) @cache = PDF::Reader::ObjectCache.new opts.merge!(:cache => @cache) @objects = PDF::Reader::ObjectHash.new(input, opts) end |
Instance Attribute Details
#objects ⇒ Object (readonly)
lowlevel hash-like access to all objects in the underlying PDF
98 99 100 |
# File 'lib/pdf/reader.rb', line 98 def objects @objects end |
Class Method Details
Instance Method Details
#info ⇒ Object
Return a Hash with some basic information about the PDF file
126 127 128 129 |
# File 'lib/pdf/reader.rb', line 126 def info dict = @objects.deref_hash(@objects.trailer[:Info]) || {} doc_strings_to_utf8(dict) end |
#metadata ⇒ Object
Return a String with extra XML metadata provided by the author of the PDF file. Not always present.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pdf/reader.rb', line 134 def stream = @objects.deref_stream(root[:Metadata]) if stream.nil? nil else xml = stream.unfiltered_data xml.force_encoding("utf-8") xml end end |
#page(num) ⇒ Object
returns a single PDF::Reader::Page for the specified page. Use this instead of pages method when you need to access just a single page
reader = PDF::Reader.new("somefile.pdf")
page = reader.page(10)
puts page.text
See the docs for PDF::Reader::Page to read more about the methods available on each page
216 217 218 219 220 221 222 |
# File 'lib/pdf/reader.rb', line 216 def page(num) num = num.to_i if num < 1 || num > self.page_count raise InvalidPageError, "Valid pages are 1 .. #{self.page_count}" end PDF::Reader::Page.new(@objects, num, :cache => @cache) end |
#page_count ⇒ Object
To number of pages in this PDF
147 148 149 150 151 152 153 |
# File 'lib/pdf/reader.rb', line 147 def page_count pages = @objects.deref_hash(root[:Pages]) unless pages.kind_of?(::Hash) raise MalformedPDFError, "Pages structure is missing #{pages.class}" end @page_count ||= @objects.deref_integer(pages[:Count]) || 0 end |
#pages ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/pdf/reader.rb', line 192 def pages return [] if page_count <= 0 (1..self.page_count).map do |num| begin PDF::Reader::Page.new(@objects, num, :cache => @cache) rescue InvalidPageError raise MalformedPDFError, "Missing data for page: #{num}" end end end |
#pdf_version ⇒ Object
The PDF version this file uses
157 158 159 |
# File 'lib/pdf/reader.rb', line 157 def pdf_version @objects.pdf_version end |