Class: Podoff::Document
- Inherits:
-
Object
- Object
- Podoff::Document
- Defined in:
- lib/podoff.rb
Instance Attribute Summary collapse
-
#additions ⇒ Object
readonly
Returns the value of attribute additions.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#obj_counters ⇒ Object
readonly
Returns the value of attribute obj_counters.
-
#objs ⇒ Object
readonly
Returns the value of attribute objs.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#scanner ⇒ Object
readonly
Returns the value of attribute scanner.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#xref ⇒ Object
readonly
Returns the value of attribute xref.
Class Method Summary collapse
Instance Method Summary collapse
- #add(obj) ⇒ Object
- #add_base_font(name) ⇒ Object
- #add_stream(src = nil, &block) ⇒ Object
- #dup ⇒ Object
-
#initialize(s, encoding) ⇒ Document
constructor
A new instance of Document.
- #new_ref ⇒ Object
- #page(index) ⇒ Object
- #pages ⇒ Object
- #re_add(obj_or_ref) ⇒ Object
- #rewrite(path = :string, encoding = nil) ⇒ Object
- #source ⇒ Object
- #updated? ⇒ Boolean
- #write(path = :string, encoding = nil) ⇒ Object
Constructor Details
#initialize(s, encoding) ⇒ Document
Returns a new instance of Document.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/podoff.rb', line 47 def initialize(s, encoding) fail ArgumentError.new('not a PDF file') \ unless s.match(/\A%PDF-\d+\.\d+\s/) @encoding = encoding @scanner = ::StringScanner.new(s) @version = nil @xref = nil @objs = {} @obj_counters = {} @root = nil @additions = {} @version = @scanner.scan(/%PDF-\d+\.\d+/) loop do @scanner.skip_until( /(startxref\s+\d+|\d+\s+\d+\s+obj|\/Root\s+\d+\s+\d+\s+R)/) m = @scanner.matched break unless m if m[0] == 's' @xref = m.split(' ').last.to_i elsif m[0] == '/' @root = extract_ref(m) else obj = Podoff::Obj.extract(self) @objs[obj.ref] = obj @obj_counters[obj.ref] = (@obj_counters[obj.ref] || 0) + 1 end end if @root == nil @scanner.pos = 0 loop do @scanner.skip_until(/\/Root\s+\d+\s+\d+\s+R/) break unless @scanner.matched @root = extract_ref(@scanner.matched) end end end |
Instance Attribute Details
#additions ⇒ Object (readonly)
Returns the value of attribute additions.
45 46 47 |
# File 'lib/podoff.rb', line 45 def additions @additions end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
36 37 38 |
# File 'lib/podoff.rb', line 36 def encoding @encoding end |
#obj_counters ⇒ Object (readonly)
Returns the value of attribute obj_counters.
42 43 44 |
# File 'lib/podoff.rb', line 42 def obj_counters @obj_counters end |
#objs ⇒ Object (readonly)
Returns the value of attribute objs.
41 42 43 |
# File 'lib/podoff.rb', line 41 def objs @objs end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
43 44 45 |
# File 'lib/podoff.rb', line 43 def root @root end |
#scanner ⇒ Object (readonly)
Returns the value of attribute scanner.
38 39 40 |
# File 'lib/podoff.rb', line 38 def scanner @scanner end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
39 40 41 |
# File 'lib/podoff.rb', line 39 def version @version end |
#xref ⇒ Object (readonly)
Returns the value of attribute xref.
40 41 42 |
# File 'lib/podoff.rb', line 40 def xref @xref end |
Class Method Details
Instance Method Details
#add(obj) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/podoff.rb', line 160 def add(obj) @objs[obj.ref] = obj @additions[obj.ref] = obj obj end |
#add_base_font(name) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/podoff.rb', line 168 def add_base_font(name) name = name[1..-1] if name[0] == '/' r = new_ref s = "#{r} obj <</Type /Font /Subtype /Type1 /BaseFont /#{name}>> endobj" add(Obj.new(self, r, source: s)) end |
#add_stream(src = nil, &block) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/podoff.rb', line 178 def add_stream(src=nil, &block) ref = new_ref src = src && [ "#{ref} obj", "<< /Length #{src.size} >>\nstream\n#{src}\nendstream", "endobj" ].join("\n") str = src ? nil : make_stream(&block) obj = add(Obj.new(self, ref, source: src, stream: str)) str || obj end |
#dup ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/podoff.rb', line 104 def dup o = self self.class.allocate.instance_eval do @encoding = o.encoding @scanner = ::StringScanner.new(o.source) @xref = o.xref @objs = o.objs.inject({}) { |h, (k, v)| h[k] = v.dup(self); h } @obj_counters = o.obj_counters.dup @root = o.root @additions = o.additions.inject({}) { |h, (k, v)| h[k] = v.dup(self); h } self end end |
#new_ref ⇒ Object
153 154 155 156 157 158 |
# File 'lib/podoff.rb', line 153 def new_ref "#{ @objs.keys.inject(-1) { |i, r| [ i, r.split(' ').first.to_i ].max } + 1 } 0" end |
#page(index) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/podoff.rb', line 142 def page(index) if index < 0 pages[index] elsif index == 0 nil else pages[index - 1] end end |
#pages ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/podoff.rb', line 127 def pages #@objs.values.select { |o| o.type == '/Page' } ps = @objs.values.find { |o| o.type == '/Pages' } fail ArgumentError.new( "no /Pages, the PDF is not usable by Podoff as is, you have to do " + "`qpdf --object-streams=disable original.pdf unpacked.pdf` " + "and use unpacked.pdf instead of original.pdf" ) unless ps extract_refs(ps.attributes[:kids]).collect { |r| @objs[r] } end |
#re_add(obj_or_ref) ⇒ Object
200 201 202 203 204 205 206 207 |
# File 'lib/podoff.rb', line 200 def re_add(obj_or_ref) obj = obj_or_ref.is_a?(String) ? @objs[obj_or_ref] : obj_or_ref obj = obj.replicate unless obj.replica? add(obj) end |
#rewrite(path = :string, encoding = nil) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/podoff.rb', line 254 def rewrite(path=:string, encoding=nil) encoding ||= @encoding f = case path when :string, '-' then StringIO.new when String then File.open(path, 'wb') else path end f.set_encoding(encoding) v = source.match(/%PDF-\d+\.\d+/)[0] f.write(v) f.write("\n") pointers = {} objs.keys.sort.each do |k| pointers[k.split(' ').first.to_i] = f.pos f.write(objs[k].source.force_encoding(encoding)) f.write("\n") end xref = f.pos write_xref(f, pointers) f.write("trailer\n") f.write("<<\n") f.write("/Size #{objs.size + 1}\n") f.write("/Root #{root} R\n") f.write(">>\n") f.write("startxref #{xref}\n") f.write("%%EOF\n") f.close if path.is_a?(String) || path.is_a?(Symbol) f.is_a?(StringIO) ? f.string : nil end |
#source ⇒ Object
94 95 96 97 |
# File 'lib/podoff.rb', line 94 def source @scanner.string end |
#updated? ⇒ Boolean
99 100 101 102 |
# File 'lib/podoff.rb', line 99 def updated? @additions.any? end |
#write(path = :string, encoding = nil) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/podoff.rb', line 209 def write(path=:string, encoding=nil) encoding ||= @encoding f = case path when :string, '-' then StringIO.new when String then File.open(path, 'wb') else path end f.set_encoding(encoding) # internal encoding: nil #f.set_encoding(encoding, encoding) f.write(source) if @additions.any? pointers = {} @additions.values.each do |o| f.write("\n") pointers[o.ref.split(' ').first.to_i] = f.pos f.write(o.to_s.force_encoding(encoding)) end f.write("\n\n") xref = f.pos write_xref(f, pointers) f.write("trailer\n") f.write("<<\n") f.write("/Prev #{self.xref}\n") f.write("/Size #{objs.size + 1}\n") f.write("/Root #{root} R\n") f.write(">>\n") f.write("startxref #{xref}\n") f.write("%%EOF\n") end f.close if path.is_a?(String) || path.is_a?(Symbol) f.is_a?(StringIO) ? f.string : nil end |