Class: PDF::Core::DocumentState Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/core/document_state.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Low-level PDF document representation mostly for keeping intermediate state while document is being constructed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DocumentState

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DocumentState.

Parameters:

  • options (Hash<Symbol, any>)

Options Hash (options):

  • :info (Hash)

    Document’s information dictionary

  • :print_scaling (:none, nil)

    Viewr preference for printing scaling

  • :trailer (Hash) — default: {}

    File trailer

  • :compress (Boolean) — default: false

    Whether to compress streams

  • :encrypt (Boolean) — default: false

    Whether to encrypt the document

  • :encryption_key (String) — default: nil

    Encryption key. Must be provided if :encrypt is true

Source Code
lib/pdf/core/document_state.rb, line 20
20
def initialize(options)
21
  normalize_metadata(options)
22
23
  @store =
24
    if options[:print_scaling]
25
      PDF::Core::ObjectStore.new(
26
        info: options[:info],
27
        print_scaling: options[:print_scaling],
28
      )
29
    else
30
      PDF::Core::ObjectStore.new(info: options[:info])
31
    end
32
33
  @version = 1.3
34
  @pages = []
35
  @page = nil
36
  @trailer = options.fetch(:trailer, {})
37
  @compress = options.fetch(:compress, false)
38
  @encrypt = options.fetch(:encrypt, false)
39
  @encryption_key = options[:encryption_key]
40
  @skip_encoding = options.fetch(:skip_encoding, false)
41
  @before_render_callbacks = []
42
  @on_page_create_callback = nil
43
end

Instance Attribute Details

#before_render_callbacksArray<Proc>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Before render callbacks

Returns:

  • (Array<Proc>)
Source Code
lib/pdf/core/document_state.rb, line 82
82
def before_render_callbacks
83
  @before_render_callbacks
84
end

#compressBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether to compress streams

Returns:

  • (Boolean)
Source Code
lib/pdf/core/document_state.rb, line 67
67
def compress
68
  @compress
69
end

#encryptBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether to encrypt document

Returns:

  • (Boolean)
Source Code
lib/pdf/core/document_state.rb, line 71
71
def encrypt
72
  @encrypt
73
end

#encryption_keyString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encryption key

Returns:

  • (String, nil)
Source Code
lib/pdf/core/document_state.rb, line 75
75
def encryption_key
76
  @encryption_key
77
end

#on_page_create_callbackProc?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A block to call when a new page is created

Returns:

  • (Proc, nil)
Source Code
lib/pdf/core/document_state.rb, line 86
86
def on_page_create_callback
87
  @on_page_create_callback
88
end

#pagePDF::Core::Page

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Current page

Returns:

Source Code
lib/pdf/core/document_state.rb, line 59
59
def page
60
  @page
61
end

#pagesArray<PDF::Core::Page>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Document pages

Returns:

Source Code
lib/pdf/core/document_state.rb, line 55
55
def pages
56
  @pages
57
end

#skip_encodingObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

Unused

Source Code
lib/pdf/core/document_state.rb, line 78
78
def skip_encoding
79
  @skip_encoding
80
end

#storePDF::Core::ObjectStore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Object store

Source Code
lib/pdf/core/document_state.rb, line 47
47
def store
48
  @store
49
end

#trailerHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Document trailer dict

Returns:

  • (Hash)
Source Code
lib/pdf/core/document_state.rb, line 63
63
def trailer
64
  @trailer
65
end

#versionFloat

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

PDF version used in this document

Returns:

  • (Float)
Source Code
lib/pdf/core/document_state.rb, line 51
51
def version
52
  @version
53
end

Instance Method Details

#before_render_actions(_doc) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Executes before render callbacks

Parameters:

  • _doc (Prawn::Document)

    Unused

Source Code
lib/pdf/core/document_state.rb, line 139
139
def before_render_actions(_doc)
140
  before_render_callbacks.each { |c| c.call(self) }
141
end

#insert_page(page, page_number) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Insert a page at the specified position.

Parameters:

Source Code
lib/pdf/core/document_state.rb, line 121
121
def insert_page(page, page_number)
122
  pages.insert(page_number, page)
123
  store.pages.data[:Kids].insert(page_number, page.dictionary)
124
  store.pages.data[:Count] += 1
125
end

#normalize_metadata(options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds Prawn metadata to document info

Parameters:

  • options (Hash)

Returns:

  • (Hash)

    Document info hash

Source Code
lib/pdf/core/document_state.rb, line 108
108
def normalize_metadata(options)
109
  options[:info] ||= {}
110
  options[:info][:Creator] ||= 'Prawn'
111
  options[:info][:Producer] ||= 'Prawn'
112
113
  options[:info]
114
end

#on_page_create_action(doc) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Execute page creation callback if one is defined

Parameters:

  • doc (Prawn::Document)
Source Code
lib/pdf/core/document_state.rb, line 131
131
def on_page_create_action(doc)
132
  on_page_create_callback[doc] if on_page_create_callback
133
end

#page_countInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Number of pages in the document

Returns:

  • (Integer)
Source Code
lib/pdf/core/document_state.rb, line 146
146
def page_count
147
  pages.length
148
end

#populate_pages_from_store(document) ⇒ 0, Array<PDF::Core::Page>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads pages from object store. Only does it when there are no pages loaded and there are some pages in the store.

Returns:

  • (0)

    if no pages were loaded

  • (Array<PDF::Core::Page>)

    if pages were laded

Source Code
lib/pdf/core/document_state.rb, line 93
93
def populate_pages_from_store(document)
94
  return 0 if @store.page_count <= 0 || !@pages.empty?
95
96
  count = (1..@store.page_count)
97
  @pages =
98
    count.map { |index|
99
      orig_dict_id = @store.object_id_for_page(index)
100
      PDF::Core::Page.new(document, object_id: orig_dict_id)
101
    }
102
end

#render_body(output) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Renders document body to the output

Parameters:

  • output (#<<)
Source Code
lib/pdf/core/document_state.rb, line 154
154
def render_body(output)
155
  store.each do |ref|
156
    ref.offset = output.size
157
    output <<
158
      if @encrypt
159
        ref.encrypted_object(@encryption_key)
160
      else
161
        ref.object
162
      end
163
  end
164
end