| 256 | } |
| 257 | |
| 258 | _finalize(fn) { |
| 259 | // generate xref |
| 260 | const xRefOffset = this._offset; |
| 261 | this._write('xref'); |
| 262 | this._write(`0 ${this._offsets.length + 1}`); |
| 263 | this._write('0000000000 65535 f '); |
| 264 | |
| 265 | for (let offset of Array.from(this._offsets)) { |
| 266 | offset = `0000000000${offset}`.slice(-10); |
| 267 | this._write(offset + ' 00000 n '); |
| 268 | } |
| 269 | |
| 270 | // trailer |
| 271 | this._write('trailer'); |
| 272 | this._write( |
| 273 | PDFObject.convert({ |
| 274 | Size: this._offsets.length + 1, |
| 275 | Root: this._root, |
| 276 | Info: this._info |
| 277 | }) |
| 278 | ); |
| 279 | |
| 280 | this._write('startxref'); |
| 281 | this._write(`${xRefOffset}`); |
| 282 | this._write('%%EOF'); |
| 283 | |
| 284 | // end the stream |
| 285 | return this.push(null); |
| 286 | } |
| 287 | |
| 288 | toString() { |
| 289 | return '[object PDFDocument]'; |