Class: DPTM6::PDF::File

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

Constant Summary collapse

@@obj0_pages =
nil
@@obj0_info =
nil
@@obj0_root =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, mode) ⇒ File

Returns a new instance of File.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dptm6/pdf.rb', line 24

def initialize(file, mode)
  @fp = Kernel.open(file, mode)

  if (mode[0] == "r")
    read_xref
    read_info
  else
    @xref = []
    @pages = []
  end
end

Instance Attribute Details

#fpObject (readonly)

Returns the value of attribute fp.



8
9
10
# File 'lib/dptm6/pdf.rb', line 8

def fp
  @fp
end

#pagesObject

Returns the value of attribute pages.



9
10
11
# File 'lib/dptm6/pdf.rb', line 9

def pages
  @pages
end

#trailerObject (readonly)

Returns the value of attribute trailer.



8
9
10
# File 'lib/dptm6/pdf.rb', line 8

def trailer
  @trailer
end

#xrefObject

Returns the value of attribute xref.



9
10
11
# File 'lib/dptm6/pdf.rb', line 9

def xref
  @xref
end

Class Method Details

.create(file) ⇒ Object



20
21
22
# File 'lib/dptm6/pdf.rb', line 20

def self.create(file)
  new(file, "wb")
end

.open(file) ⇒ Object



16
17
18
# File 'lib/dptm6/pdf.rb', line 16

def self.open(file)
  new(file, "rb")
end

Instance Method Details

#closeObject



36
37
38
# File 'lib/dptm6/pdf.rb', line 36

def close
  @fp.close
end

#get_dclimage(num) ⇒ Object



40
41
42
# File 'lib/dptm6/pdf.rb', line 40

def get_dclimage(num)
  PDF::DCLImage.new(self, num)
end

#get_object(num, type = PDF::Object) ⇒ Object



44
45
46
# File 'lib/dptm6/pdf.rb', line 44

def get_object(num, type = PDF::Object)
  type.new(self, num)
end

#get_object_by_key(dict, key, type = PDF::Object) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/dptm6/pdf.rb', line 48

def get_object_by_key(dict, key, type = PDF::Object)
  ary = dict.scan(/#{key}\s++(\d++)\s++\d++\s++R/m)
  return nil if ary.empty?

  num = ary[0][0].to_i
  get_object(num, type)
end

#write_infoObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/dptm6/pdf.rb', line 56

def write_info
  str = @pages.collect { |i| "#{i} 0 R " }.join
  @@obj0_pages.data[/\/Kids\s++\[\s++([\d\sR]++)\]/,1] = str
  @@obj0_pages.data[/\/Count\s++(\d++)/,1] = @pages.size.to_s
  @@obj0_pages.move_to(self, 1).write

  @@obj0_info.move_to(self, @xref.size).write

  @@obj0_root.move_to(self, @xref.size).write
end

#write_xrefObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dptm6/pdf.rb', line 67

def write_xref
  pos = @fp.pos
  size = @xref.size

  @fp.write("xref\n")
  @fp.write("#{0} #{size}\n")
  @xref.sort_by(&:last).each do |i,j,k|
    @fp.write("%010d %05d %c \n" % (k == 0 ? [i, 65535, "f"] : [i, 0, "n"]))
  end

  @fp.write("trailer\n<< /Size \#{size}\n   /Root \#{size-1} 0 R\n   /Info \#{size-2} 0 R\n>>\n  EOS\n\n  @fp.write(<<-EOS)\nstartxref\n\#{pos}\n%%EOF\n  EOS\nend\n")