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`



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pdf/core/document_state.rb', line 20

def initialize(options)
  (options)

  @store =
    if options[:print_scaling]
      PDF::Core::ObjectStore.new(
        info: options[:info],
        print_scaling: options[:print_scaling],
      )
    else
      PDF::Core::ObjectStore.new(info: options[:info])
    end

  @version = 1.3
  @pages = []
  @page = nil
  @trailer = options.fetch(:trailer, {})
  @compress = options.fetch(:compress, false)
  @encrypt = options.fetch(:encrypt, false)
  @encryption_key = options[:encryption_key]
  @skip_encoding = options.fetch(:skip_encoding, false)
  @before_render_callbacks = []
  @on_page_create_callback = nil
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>)


82
83
84
# File 'lib/pdf/core/document_state.rb', line 82

def before_render_callbacks
  @before_render_callbacks
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)


67
68
69
# File 'lib/pdf/core/document_state.rb', line 67

def compress
  @compress
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)


71
72
73
# File 'lib/pdf/core/document_state.rb', line 71

def encrypt
  @encrypt
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)


75
76
77
# File 'lib/pdf/core/document_state.rb', line 75

def encryption_key
  @encryption_key
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)


86
87
88
# File 'lib/pdf/core/document_state.rb', line 86

def on_page_create_callback
  @on_page_create_callback
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:



59
60
61
# File 'lib/pdf/core/document_state.rb', line 59

def page
  @page
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:



55
56
57
# File 'lib/pdf/core/document_state.rb', line 55

def pages
  @pages
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



78
79
80
# File 'lib/pdf/core/document_state.rb', line 78

def skip_encoding
  @skip_encoding
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



47
48
49
# File 'lib/pdf/core/document_state.rb', line 47

def store
  @store
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)


63
64
65
# File 'lib/pdf/core/document_state.rb', line 63

def trailer
  @trailer
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)


51
52
53
# File 'lib/pdf/core/document_state.rb', line 51

def version
  @version
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



139
140
141
# File 'lib/pdf/core/document_state.rb', line 139

def before_render_actions(_doc)
  before_render_callbacks.each { |c| c.call(self) }
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:



121
122
123
124
125
# File 'lib/pdf/core/document_state.rb', line 121

def insert_page(page, page_number)
  pages.insert(page_number, page)
  store.pages.data[:Kids].insert(page_number, page.dictionary)
  store.pages.data[:Count] += 1
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



108
109
110
111
112
113
114
# File 'lib/pdf/core/document_state.rb', line 108

def (options)
  options[:info] ||= {}
  options[:info][:Creator] ||= 'Prawn'
  options[:info][:Producer] ||= 'Prawn'

  options[:info]
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)


131
132
133
# File 'lib/pdf/core/document_state.rb', line 131

def on_page_create_action(doc)
  on_page_create_callback[doc] if on_page_create_callback
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)


146
147
148
# File 'lib/pdf/core/document_state.rb', line 146

def page_count
  pages.length
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



93
94
95
96
97
98
99
100
101
102
# File 'lib/pdf/core/document_state.rb', line 93

def populate_pages_from_store(document)
  return 0 if @store.page_count <= 0 || !@pages.empty?

  count = (1..@store.page_count)
  @pages =
    count.map { |index|
      orig_dict_id = @store.object_id_for_page(index)
      PDF::Core::Page.new(document, object_id: orig_dict_id)
    }
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 (#<<)


154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pdf/core/document_state.rb', line 154

def render_body(output)
  store.each do |ref|
    ref.offset = output.size
    output <<
      if @encrypt
        ref.encrypted_object(@encryption_key)
      else
        ref.object
      end
  end
end