Class: Prune::Document

Inherits:
Object
  • Object
show all
Includes:
Elements, Errors, PObjects
Defined in:
lib/prune/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PObjects

pa, pd, ph, pl, pn, ps

Constructor Details

#initializeDocument

Initialize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prune/document.rb', line 15

def initialize
  @elements = []
  @fonts = {}
  @version = "1.2"

  # Initialize PDF infos.
  @info = Info.new(self)

  # Initialize catalog.
  @catalog = Catalog.new(self)
  @catalog.version = @version

  # Initialize outlines.
  outlines = Outlines.new(self)
  @catalog.outlines = outlines.reference

  # Initialize pages.
  @pages = Pages.new(self)
  @catalog.pages = @pages.reference

  # Initialize ProcSet
  @proc_set = Elements::ProcedureSets.new(self)
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



10
11
12
# File 'lib/prune/document.rb', line 10

def catalog
  @catalog
end

#elementsObject (readonly)

Returns the value of attribute elements.



11
12
13
# File 'lib/prune/document.rb', line 11

def elements
  @elements
end

#fontsObject (readonly)

Returns the value of attribute fonts.



11
12
13
# File 'lib/prune/document.rb', line 11

def fonts
  @fonts
end

#infoObject (readonly)

Returns the value of attribute info.



10
11
12
# File 'lib/prune/document.rb', line 10

def info
  @info
end

#outlinesObject (readonly)

Returns the value of attribute outlines.



10
11
12
# File 'lib/prune/document.rb', line 10

def outlines
  @outlines
end

#pagesObject (readonly)

Returns the value of attribute pages.



10
11
12
# File 'lib/prune/document.rb', line 10

def pages
  @pages
end

#proc_setObject (readonly)

Returns the value of attribute proc_set.



10
11
12
# File 'lib/prune/document.rb', line 10

def proc_set
  @proc_set
end

#version=(value) ⇒ Object (writeonly)

Sets the attribute version

Parameters:

  • value

    the value to set the attribute version to.



12
13
14
# File 'lib/prune/document.rb', line 12

def version=(value)
  @version = value
end

Instance Method Details

#save_as(filename) ⇒ Object

Save pdf document to a file.

Raises:



40
41
42
43
44
45
46
47
48
# File 'lib/prune/document.rb', line 40

def save_as(filename)
  raise DocumentEmptyError if @pages.empty?
  # Write to a file
  File.open(filename, "wb") do |file|
    file.flock(File::LOCK_EX)
    file.write(self.to_s)
    file.flock(File::LOCK_UN)
  end
end

#to_sObject

Convert to String.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prune/document.rb', line 51

def to_s
  # Write header
  out = []
  out << "%PDF-#{@version}"
  out << "%" + [0xE2, 0xE3, 0xCF, 0xD3].pack("c*")
  # Write objects
  out += @elements.collect{|element| element.to_s}
  # Write cross-reference table
  out += cross_reference_table(@elements.size, out.join(LF))
  # Write trailer
  out += trailer(out.join(LF))
  out << "%%EOF"
  out.join(LF)
end