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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/podoff.rb', line 71 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 i = @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 i = @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.
69 70 71 |
# File 'lib/podoff.rb', line 69 def additions @additions end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
60 61 62 |
# File 'lib/podoff.rb', line 60 def encoding @encoding end |
#obj_counters ⇒ Object (readonly)
Returns the value of attribute obj_counters.
66 67 68 |
# File 'lib/podoff.rb', line 66 def obj_counters @obj_counters end |
#objs ⇒ Object (readonly)
Returns the value of attribute objs.
65 66 67 |
# File 'lib/podoff.rb', line 65 def objs @objs end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
67 68 69 |
# File 'lib/podoff.rb', line 67 def root @root end |
#scanner ⇒ Object (readonly)
Returns the value of attribute scanner.
62 63 64 |
# File 'lib/podoff.rb', line 62 def scanner @scanner end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
63 64 65 |
# File 'lib/podoff.rb', line 63 def version @version end |
#xref ⇒ Object (readonly)
Returns the value of attribute xref.
64 65 66 |
# File 'lib/podoff.rb', line 64 def xref @xref end |
Class Method Details
Instance Method Details
#add(obj) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/podoff.rb', line 184 def add(obj) @objs[obj.ref] = obj @additions[obj.ref] = obj obj end |
#add_base_font(name) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/podoff.rb', line 192 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
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/podoff.rb', line 202 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/podoff.rb', line 128 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
177 178 179 180 181 182 |
# File 'lib/podoff.rb', line 177 def new_ref "#{ @objs.keys.inject(-1) { |i, r| [ i, r.split(' ').first.to_i ].max } + 1 } 0" end |
#page(index) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/podoff.rb', line 166 def page(index) if index < 0 pages[index] elsif index == 0 nil else pages[index - 1] end end |
#pages ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/podoff.rb', line 151 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
224 225 226 227 228 229 230 231 |
# File 'lib/podoff.rb', line 224 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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/podoff.rb', line 278 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
118 119 120 121 |
# File 'lib/podoff.rb', line 118 def source @scanner.string end |
#updated? ⇒ Boolean
123 124 125 126 |
# File 'lib/podoff.rb', line 123 def updated? @additions.any? end |
#write(path = :string, encoding = nil) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/podoff.rb', line 233 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 |