pdfjs-dist 2.5.207 → 2.6.347

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pdfjs-dist might be problematic. Click here for more details.

Files changed (133) hide show
  1. package/bower.json +1 -1
  2. package/build/pdf.js +867 -216
  3. package/build/pdf.js.map +1 -1
  4. package/build/pdf.min.js +1 -1
  5. package/build/pdf.worker.js +10214 -8547
  6. package/build/pdf.worker.js.map +1 -1
  7. package/build/pdf.worker.min.js +1 -1
  8. package/es5/build/pdf.js +2023 -1046
  9. package/es5/build/pdf.js.map +1 -1
  10. package/es5/build/pdf.min.js +22 -0
  11. package/es5/build/pdf.worker.js +11136 -8144
  12. package/es5/build/pdf.worker.js.map +1 -1
  13. package/es5/build/pdf.worker.min.js +22 -0
  14. package/es5/image_decoders/pdf.image_decoders.js +12529 -0
  15. package/es5/image_decoders/pdf.image_decoders.js.map +1 -0
  16. package/es5/image_decoders/pdf.image_decoders.min.js +22 -0
  17. package/es5/web/images/shadow.png +0 -0
  18. package/es5/web/pdf_viewer.css +8 -5
  19. package/es5/web/pdf_viewer.js +195 -51
  20. package/es5/web/pdf_viewer.js.map +1 -1
  21. package/image_decoders/pdf.image_decoders.js +24 -16
  22. package/image_decoders/pdf.image_decoders.js.map +1 -1
  23. package/image_decoders/pdf.image_decoders.min.js +1 -1
  24. package/lib/core/annotation.js +479 -43
  25. package/lib/core/cff_parser.js +643 -615
  26. package/lib/core/chunked_stream.js +64 -50
  27. package/lib/core/colorspace.js +126 -104
  28. package/lib/core/crypto.js +37 -0
  29. package/lib/core/document.js +128 -75
  30. package/lib/core/evaluator.js +3015 -2754
  31. package/lib/core/fonts.js +47 -46
  32. package/lib/core/function.js +79 -6
  33. package/lib/core/image.js +364 -357
  34. package/lib/core/image_utils.js +109 -8
  35. package/lib/core/jpg.js +8 -3
  36. package/lib/core/obj.js +255 -34
  37. package/lib/core/operator_list.js +17 -15
  38. package/lib/core/parser.js +40 -3
  39. package/lib/core/pattern.js +24 -11
  40. package/lib/core/primitives.js +112 -53
  41. package/lib/core/type1_parser.js +2 -0
  42. package/lib/core/worker.js +102 -37
  43. package/lib/core/writer.js +242 -0
  44. package/lib/display/annotation_layer.js +51 -10
  45. package/lib/display/annotation_storage.js +89 -0
  46. package/lib/display/api.js +140 -54
  47. package/lib/display/canvas.js +215 -46
  48. package/lib/display/display_utils.js +55 -18
  49. package/lib/display/fetch_stream.js +6 -0
  50. package/lib/display/font_loader.js +26 -15
  51. package/lib/display/metadata.js +1 -7
  52. package/lib/display/network.js +0 -14
  53. package/lib/display/node_utils.js +87 -0
  54. package/lib/display/optional_content_config.js +184 -0
  55. package/lib/display/svg.js +1 -1
  56. package/lib/display/text_layer.js +4 -1
  57. package/lib/pdf.js +2 -2
  58. package/lib/pdf.worker.js +2 -2
  59. package/lib/shared/is_node.js +1 -1
  60. package/lib/shared/util.js +13 -10
  61. package/lib/test/unit/annotation_spec.js +520 -50
  62. package/lib/test/unit/annotation_storage_spec.js +83 -0
  63. package/lib/test/unit/api_spec.js +16 -6
  64. package/lib/test/unit/cmap_spec.js +4 -4
  65. package/lib/test/unit/colorspace_spec.js +216 -24
  66. package/lib/test/unit/crypto_spec.js +51 -4
  67. package/lib/test/unit/custom_spec.js +129 -1
  68. package/lib/test/unit/document_spec.js +112 -1
  69. package/lib/test/unit/evaluator_spec.js +29 -10
  70. package/lib/test/unit/jasmine-boot.js +1 -1
  71. package/lib/test/unit/metadata_spec.js +3 -3
  72. package/lib/test/unit/primitives_spec.js +242 -63
  73. package/lib/test/unit/test_utils.js +42 -89
  74. package/lib/test/unit/testreporter.js +17 -6
  75. package/lib/test/unit/util_spec.js +13 -12
  76. package/lib/test/unit/writer_spec.js +84 -0
  77. package/lib/web/annotation_layer_builder.js +13 -9
  78. package/lib/web/app.js +231 -26
  79. package/lib/web/app_options.js +1 -6
  80. package/lib/web/base_tree_viewer.js +100 -0
  81. package/lib/web/base_viewer.js +53 -4
  82. package/lib/web/download_manager.js +3 -10
  83. package/lib/web/firefox_print_service.js +32 -12
  84. package/lib/web/firefoxcom.js +20 -6
  85. package/lib/web/interfaces.js +1 -1
  86. package/lib/web/pdf_attachment_viewer.js +80 -38
  87. package/lib/web/pdf_find_controller.js +2 -1
  88. package/lib/web/pdf_layer_viewer.js +208 -0
  89. package/lib/web/pdf_outline_viewer.js +24 -57
  90. package/lib/web/pdf_page_view.js +29 -6
  91. package/lib/web/pdf_print_service.js +13 -11
  92. package/lib/web/pdf_sidebar.js +46 -25
  93. package/lib/web/pdf_thumbnail_view.js +16 -3
  94. package/lib/web/pdf_thumbnail_viewer.js +17 -1
  95. package/lib/web/pdf_viewer.component.js +2 -2
  96. package/lib/web/preferences.js +1 -1
  97. package/lib/web/toolbar.js +1 -1
  98. package/lib/web/ui_utils.js +7 -1
  99. package/package.json +3 -1
  100. package/types/display/annotation_layer.d.ts +61 -0
  101. package/types/display/annotation_storage.d.ts +36 -0
  102. package/types/display/api.d.ts +1107 -0
  103. package/types/display/api_compatibility.d.ts +1 -0
  104. package/types/display/canvas.d.ts +4 -0
  105. package/types/display/content_disposition.d.ts +7 -0
  106. package/types/display/display_utils.d.ts +243 -0
  107. package/types/display/fetch_stream.d.ts +67 -0
  108. package/types/display/font_loader.d.ts +19 -0
  109. package/types/display/metadata.d.ts +9 -0
  110. package/types/display/network.d.ts +80 -0
  111. package/types/display/network_utils.d.ts +14 -0
  112. package/types/display/node_stream.d.ts +72 -0
  113. package/types/display/node_utils.d.ts +6 -0
  114. package/types/display/optional_content_config.d.ts +12 -0
  115. package/types/display/pattern_helper.d.ts +5 -0
  116. package/types/display/svg.d.ts +2 -0
  117. package/types/display/text_layer.d.ts +83 -0
  118. package/types/display/transport_stream.d.ts +57 -0
  119. package/types/display/webgl.d.ts +20 -0
  120. package/types/display/worker_options.d.ts +27 -0
  121. package/types/display/xml_parser.d.ts +35 -0
  122. package/types/pdf.d.ts +33 -0
  123. package/types/shared/compatibility.d.ts +1 -0
  124. package/types/shared/is_node.d.ts +1 -0
  125. package/types/shared/message_handler.d.ts +62 -0
  126. package/types/shared/util.d.ts +421 -0
  127. package/web/images/shadow.png +0 -0
  128. package/web/pdf_viewer.css +8 -5
  129. package/web/pdf_viewer.js +109 -33
  130. package/web/pdf_viewer.js.map +1 -1
  131. package/webpack.js +1 -9
  132. package/es5/web/images/texture.png +0 -0
  133. package/web/images/texture.png +0 -0
@@ -196,10 +196,11 @@ class Parser {
196
196
  I = 0x49,
197
197
  SPACE = 0x20,
198
198
  LF = 0xa,
199
- CR = 0xd;
200
- const n = 10,
199
+ CR = 0xd,
201
200
  NUL = 0x0;
202
- const startPos = stream.pos;
201
+ const lexer = this.lexer,
202
+ startPos = stream.pos,
203
+ n = 10;
203
204
  let state = 0,
204
205
  ch,
205
206
  maybeEIPos;
@@ -229,6 +230,20 @@ class Parser {
229
230
  }
230
231
  }
231
232
 
233
+ if (state !== 2) {
234
+ continue;
235
+ }
236
+
237
+ if (lexer.knownCommands) {
238
+ const nextObj = lexer.peekObj();
239
+
240
+ if (nextObj instanceof _primitives.Cmd && !lexer.knownCommands[nextObj.cmd]) {
241
+ state = 0;
242
+ }
243
+ } else {
244
+ (0, _util.warn)("findDefaultInlineStreamEnd - `lexer.knownCommands` is undefined.");
245
+ }
246
+
232
247
  if (state === 2) {
233
248
  break;
234
249
  }
@@ -1234,6 +1249,28 @@ class Lexer {
1234
1249
  return _primitives.Cmd.get(str);
1235
1250
  }
1236
1251
 
1252
+ peekObj() {
1253
+ const streamPos = this.stream.pos,
1254
+ currentChar = this.currentChar,
1255
+ beginInlineImagePos = this.beginInlineImagePos;
1256
+ let nextObj;
1257
+
1258
+ try {
1259
+ nextObj = this.getObj();
1260
+ } catch (ex) {
1261
+ if (ex instanceof _core_utils.MissingDataException) {
1262
+ throw ex;
1263
+ }
1264
+
1265
+ (0, _util.warn)(`peekObj: ${ex}`);
1266
+ }
1267
+
1268
+ this.stream.pos = streamPos;
1269
+ this.currentChar = currentChar;
1270
+ this.beginInlineImagePos = beginInlineImagePos;
1271
+ return nextObj;
1272
+ }
1273
+
1237
1274
  skipToNextLine() {
1238
1275
  let ch = this.currentChar;
1239
1276
 
@@ -56,7 +56,7 @@ var Pattern = function PatternClosure() {
56
56
  }
57
57
  };
58
58
 
59
- Pattern.parseShading = function (shading, matrix, xref, res, handler, pdfFunctionFactory) {
59
+ Pattern.parseShading = function (shading, matrix, xref, res, handler, pdfFunctionFactory, localColorSpaceCache) {
60
60
  var dict = (0, _primitives.isStream)(shading) ? shading.dict : shading;
61
61
  var type = dict.get("ShadingType");
62
62
 
@@ -64,13 +64,13 @@ var Pattern = function PatternClosure() {
64
64
  switch (type) {
65
65
  case ShadingType.AXIAL:
66
66
  case ShadingType.RADIAL:
67
- return new Shadings.RadialAxial(dict, matrix, xref, res, pdfFunctionFactory);
67
+ return new Shadings.RadialAxial(dict, matrix, xref, res, pdfFunctionFactory, localColorSpaceCache);
68
68
 
69
69
  case ShadingType.FREE_FORM_MESH:
70
70
  case ShadingType.LATTICE_FORM_MESH:
71
71
  case ShadingType.COONS_PATCH_MESH:
72
72
  case ShadingType.TENSOR_PATCH_MESH:
73
- return new Shadings.Mesh(shading, matrix, xref, res, pdfFunctionFactory);
73
+ return new Shadings.Mesh(shading, matrix, xref, res, pdfFunctionFactory, localColorSpaceCache);
74
74
 
75
75
  default:
76
76
  throw new _util.FormatError("Unsupported ShadingType: " + type);
@@ -96,13 +96,20 @@ var Shadings = {};
96
96
  Shadings.SMALL_NUMBER = 1e-6;
97
97
 
98
98
  Shadings.RadialAxial = function RadialAxialClosure() {
99
- function RadialAxial(dict, matrix, xref, res, pdfFunctionFactory) {
99
+ function RadialAxial(dict, matrix, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
100
100
  this.matrix = matrix;
101
101
  this.coordsArr = dict.getArray("Coords");
102
102
  this.shadingType = dict.get("ShadingType");
103
103
  this.type = "Pattern";
104
- var cs = dict.get("ColorSpace", "CS");
105
- cs = _colorspace.ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
104
+
105
+ const cs = _colorspace.ColorSpace.parse({
106
+ cs: dict.getRaw("ColorSpace") || dict.getRaw("CS"),
107
+ xref,
108
+ resources,
109
+ pdfFunctionFactory,
110
+ localColorSpaceCache
111
+ });
112
+
106
113
  this.cs = cs;
107
114
  const bbox = dict.getArray("BBox");
108
115
 
@@ -146,7 +153,7 @@ Shadings.RadialAxial = function RadialAxialClosure() {
146
153
 
147
154
  this.extendStart = extendStart;
148
155
  this.extendEnd = extendEnd;
149
- var fnObj = dict.get("Function");
156
+ var fnObj = dict.getRaw("Function");
150
157
  var fn = pdfFunctionFactory.createFromArray(fnObj);
151
158
  const NUMBER_OF_SAMPLES = 10;
152
159
  const step = (t1 - t0) / NUMBER_OF_SAMPLES;
@@ -835,7 +842,7 @@ Shadings.Mesh = function MeshClosure() {
835
842
  }
836
843
  }
837
844
 
838
- function Mesh(stream, matrix, xref, res, pdfFunctionFactory) {
845
+ function Mesh(stream, matrix, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
839
846
  if (!(0, _primitives.isStream)(stream)) {
840
847
  throw new _util.FormatError("Mesh data is not a stream");
841
848
  }
@@ -852,11 +859,17 @@ Shadings.Mesh = function MeshClosure() {
852
859
  this.bbox = null;
853
860
  }
854
861
 
855
- var cs = dict.get("ColorSpace", "CS");
856
- cs = _colorspace.ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
862
+ const cs = _colorspace.ColorSpace.parse({
863
+ cs: dict.getRaw("ColorSpace") || dict.getRaw("CS"),
864
+ xref,
865
+ resources,
866
+ pdfFunctionFactory,
867
+ localColorSpaceCache
868
+ });
869
+
857
870
  this.cs = cs;
858
871
  this.background = dict.has("Background") ? cs.getRgb(dict.get("Background"), 0) : null;
859
- var fnObj = dict.get("Function");
872
+ var fnObj = dict.getRaw("Function");
860
873
  var fn = fnObj ? pdfFunctionFactory.createFromArray(fnObj) : null;
861
874
  this.coords = [];
862
875
  this.colors = [];
@@ -103,6 +103,10 @@ var Dict = function DictClosure() {
103
103
  this.xref = newXref;
104
104
  },
105
105
 
106
+ get size() {
107
+ return Object.keys(this._map).length;
108
+ },
109
+
106
110
  get(key1, key2, key3) {
107
111
  let value = this._map[key1];
108
112
 
@@ -165,6 +169,9 @@ var Dict = function DictClosure() {
165
169
  getKeys: function Dict_getKeys() {
166
170
  return Object.keys(this._map);
167
171
  },
172
+ getRawValues: function Dict_getRawValues() {
173
+ return Object.values(this._map);
174
+ },
168
175
  set: function Dict_set(key, value) {
169
176
  this._map[key] = value;
170
177
  },
@@ -179,26 +186,75 @@ var Dict = function DictClosure() {
179
186
  };
180
187
  Dict.empty = new Dict(null);
181
188
 
182
- Dict.merge = function (xref, dictArray) {
189
+ Dict.merge = function ({
190
+ xref,
191
+ dictArray,
192
+ mergeSubDicts = false
193
+ }) {
183
194
  const mergedDict = new Dict(xref);
184
195
 
185
- for (let i = 0, ii = dictArray.length; i < ii; i++) {
186
- const dict = dictArray[i];
196
+ if (!mergeSubDicts) {
197
+ for (const dict of dictArray) {
198
+ if (!(dict instanceof Dict)) {
199
+ continue;
200
+ }
201
+
202
+ for (const [key, value] of Object.entries(dict._map)) {
203
+ if (mergedDict._map[key] === undefined) {
204
+ mergedDict._map[key] = value;
205
+ }
206
+ }
207
+ }
208
+
209
+ return mergedDict.size > 0 ? mergedDict : Dict.empty;
210
+ }
211
+
212
+ const properties = new Map();
187
213
 
188
- if (!isDict(dict)) {
214
+ for (const dict of dictArray) {
215
+ if (!(dict instanceof Dict)) {
189
216
  continue;
190
217
  }
191
218
 
192
- for (const keyName in dict._map) {
193
- if (mergedDict._map[keyName] !== undefined) {
219
+ for (const [key, value] of Object.entries(dict._map)) {
220
+ let property = properties.get(key);
221
+
222
+ if (property === undefined) {
223
+ property = [];
224
+ properties.set(key, property);
225
+ }
226
+
227
+ property.push(value);
228
+ }
229
+ }
230
+
231
+ for (const [name, values] of properties) {
232
+ if (values.length === 1 || !(values[0] instanceof Dict)) {
233
+ mergedDict._map[name] = values[0];
234
+ continue;
235
+ }
236
+
237
+ const subDict = new Dict(xref);
238
+
239
+ for (const dict of values) {
240
+ if (!(dict instanceof Dict)) {
194
241
  continue;
195
242
  }
196
243
 
197
- mergedDict._map[keyName] = dict._map[keyName];
244
+ for (const [key, value] of Object.entries(dict._map)) {
245
+ if (subDict._map[key] === undefined) {
246
+ subDict._map[key] = value;
247
+ }
248
+ }
249
+ }
250
+
251
+ if (subDict.size > 0) {
252
+ mergedDict._map[name] = subDict;
198
253
  }
199
254
  }
200
255
 
201
- return mergedDict;
256
+ properties.clear();
257
+ return mergedDict.size > 0 ? mergedDict : Dict.empty;
202
258
  };
203
259
 
204
260
  return Dict;
@@ -239,60 +295,63 @@ var Ref = function RefClosure() {
239
295
 
240
296
  exports.Ref = Ref;
241
297
 
242
- var RefSet = function RefSetClosure() {
243
- function RefSet() {
244
- this.dict = Object.create(null);
298
+ class RefSet {
299
+ constructor() {
300
+ this._set = new Set();
245
301
  }
246
302
 
247
- RefSet.prototype = {
248
- has: function RefSet_has(ref) {
249
- return ref.toString() in this.dict;
250
- },
251
- put: function RefSet_put(ref) {
252
- this.dict[ref.toString()] = true;
253
- },
254
- remove: function RefSet_remove(ref) {
255
- delete this.dict[ref.toString()];
256
- }
257
- };
258
- return RefSet;
259
- }();
303
+ has(ref) {
304
+ return this._set.has(ref.toString());
305
+ }
306
+
307
+ put(ref) {
308
+ this._set.add(ref.toString());
309
+ }
310
+
311
+ remove(ref) {
312
+ this._set.delete(ref.toString());
313
+ }
314
+
315
+ }
260
316
 
261
317
  exports.RefSet = RefSet;
262
318
 
263
- var RefSetCache = function RefSetCacheClosure() {
264
- function RefSetCache() {
265
- this.dict = Object.create(null);
319
+ class RefSetCache {
320
+ constructor() {
321
+ this._map = new Map();
266
322
  }
267
323
 
268
- RefSetCache.prototype = {
269
- get size() {
270
- return Object.keys(this.dict).length;
271
- },
324
+ get size() {
325
+ return this._map.size;
326
+ }
272
327
 
273
- get: function RefSetCache_get(ref) {
274
- return this.dict[ref.toString()];
275
- },
276
- has: function RefSetCache_has(ref) {
277
- return ref.toString() in this.dict;
278
- },
279
- put: function RefSetCache_put(ref, obj) {
280
- this.dict[ref.toString()] = obj;
281
- },
282
- putAlias: function RefSetCache_putAlias(ref, aliasRef) {
283
- this.dict[ref.toString()] = this.get(aliasRef);
284
- },
285
- forEach: function RefSetCache_forEach(callback) {
286
- for (const i in this.dict) {
287
- callback(this.dict[i]);
288
- }
289
- },
290
- clear: function RefSetCache_clear() {
291
- this.dict = Object.create(null);
328
+ get(ref) {
329
+ return this._map.get(ref.toString());
330
+ }
331
+
332
+ has(ref) {
333
+ return this._map.has(ref.toString());
334
+ }
335
+
336
+ put(ref, obj) {
337
+ this._map.set(ref.toString(), obj);
338
+ }
339
+
340
+ putAlias(ref, aliasRef) {
341
+ this._map.set(ref.toString(), this.get(aliasRef));
342
+ }
343
+
344
+ forEach(callback) {
345
+ for (const value of this._map.values()) {
346
+ callback(value);
292
347
  }
293
- };
294
- return RefSetCache;
295
- }();
348
+ }
349
+
350
+ clear() {
351
+ this._map.clear();
352
+ }
353
+
354
+ }
296
355
 
297
356
  exports.RefSetCache = RefSetCache;
298
357
 
@@ -217,7 +217,9 @@ var Type1CharString = function Type1CharStringClosure() {
217
217
 
218
218
  case (12 << 8) + 6:
219
219
  if (seacAnalysisEnabled) {
220
+ const asb = this.stack[this.stack.length - 5];
220
221
  this.seac = this.stack.splice(-4, 4);
222
+ this.seac[0] += this.lsb - asb;
221
223
  error = this.executeCommand(0, COMMAND_MAP.endchar);
222
224
  } else {
223
225
  error = this.executeCommand(4, COMMAND_MAP.endchar);
@@ -32,6 +32,8 @@ var _primitives = require("./primitives.js");
32
32
 
33
33
  var _pdf_manager = require("./pdf_manager.js");
34
34
 
35
+ var _writer = require("./writer.js");
36
+
35
37
  var _is_node = require("../shared/is_node.js");
36
38
 
37
39
  var _message_handler = require("../shared/message_handler.js");
@@ -40,39 +42,37 @@ var _worker_stream = require("./worker_stream.js");
40
42
 
41
43
  var _core_utils = require("./core_utils.js");
42
44
 
43
- var WorkerTask = function WorkerTaskClosure() {
44
- function WorkerTask(name) {
45
+ class WorkerTask {
46
+ constructor(name) {
45
47
  this.name = name;
46
48
  this.terminated = false;
47
49
  this._capability = (0, _util.createPromiseCapability)();
48
50
  }
49
51
 
50
- WorkerTask.prototype = {
51
- get finished() {
52
- return this._capability.promise;
53
- },
52
+ get finished() {
53
+ return this._capability.promise;
54
+ }
54
55
 
55
- finish() {
56
- this._capability.resolve();
57
- },
56
+ finish() {
57
+ this._capability.resolve();
58
+ }
58
59
 
59
- terminate() {
60
- this.terminated = true;
61
- },
60
+ terminate() {
61
+ this.terminated = true;
62
+ }
62
63
 
63
- ensureNotTerminated() {
64
- if (this.terminated) {
65
- throw new Error("Worker task was terminated");
66
- }
64
+ ensureNotTerminated() {
65
+ if (this.terminated) {
66
+ throw new Error("Worker task was terminated");
67
67
  }
68
+ }
68
69
 
69
- };
70
- return WorkerTask;
71
- }();
70
+ }
72
71
 
73
72
  exports.WorkerTask = WorkerTask;
74
- var WorkerMessageHandler = {
75
- setup(handler, port) {
73
+
74
+ class WorkerMessageHandler {
75
+ static setup(handler, port) {
76
76
  var testMessageProcessed = false;
77
77
  handler.on("test", function wphSetupTest(data) {
78
78
  if (testMessageProcessed) {
@@ -98,16 +98,16 @@ var WorkerMessageHandler = {
98
98
  handler.on("GetDocRequest", function wphSetupDoc(data) {
99
99
  return WorkerMessageHandler.createDocumentHandler(data, port);
100
100
  });
101
- },
101
+ }
102
102
 
103
- createDocumentHandler(docParams, port) {
103
+ static createDocumentHandler(docParams, port) {
104
104
  var pdfManager;
105
105
  var terminated = false;
106
106
  var cancelXHRs = null;
107
107
  var WorkerTasks = [];
108
108
  const verbosity = (0, _util.getVerbosityLevel)();
109
109
  const apiVersion = docParams.apiVersion;
110
- const workerVersion = '2.5.207';
110
+ const workerVersion = '2.6.347';
111
111
 
112
112
  if (apiVersion !== workerVersion) {
113
113
  throw new Error(`The API version "${apiVersion}" does not match ` + `the Worker version "${workerVersion}".`);
@@ -321,11 +321,11 @@ var WorkerMessageHandler = {
321
321
 
322
322
  function pdfManagerReady() {
323
323
  ensureNotTerminated();
324
- loadDocument(false).then(onSuccess, function loadFailure(ex) {
324
+ loadDocument(false).then(onSuccess, function (reason) {
325
325
  ensureNotTerminated();
326
326
 
327
- if (!(ex instanceof _core_utils.XRefParseException)) {
328
- onFailure(ex);
327
+ if (!(reason instanceof _core_utils.XRefParseException)) {
328
+ onFailure(reason);
329
329
  return;
330
330
  }
331
331
 
@@ -334,7 +334,7 @@ var WorkerMessageHandler = {
334
334
  ensureNotTerminated();
335
335
  loadDocument(true).then(onSuccess, onFailure);
336
336
  });
337
- }, onFailure);
337
+ });
338
338
  }
339
339
 
340
340
  ensureNotTerminated();
@@ -372,11 +372,12 @@ var WorkerMessageHandler = {
372
372
  });
373
373
  });
374
374
  });
375
- handler.on("GetPageIndex", function wphSetupGetPageIndex(data) {
376
- var ref = _primitives.Ref.get(data.ref.num, data.ref.gen);
375
+ handler.on("GetPageIndex", function wphSetupGetPageIndex({
376
+ ref
377
+ }) {
378
+ const pageRef = _primitives.Ref.get(ref.num, ref.gen);
377
379
 
378
- var catalog = pdfManager.pdfDocument.catalog;
379
- return catalog.getPageIndex(ref);
380
+ return pdfManager.ensureCatalog("getPageIndex", [pageRef]);
380
381
  });
381
382
  handler.on("GetDestinations", function wphSetupGetDestinations(data) {
382
383
  return pdfManager.ensureCatalog("destinations");
@@ -408,6 +409,9 @@ var WorkerMessageHandler = {
408
409
  handler.on("GetOutline", function wphSetupGetOutline(data) {
409
410
  return pdfManager.ensureCatalog("documentOutline");
410
411
  });
412
+ handler.on("GetOptionalContentConfig", function (data) {
413
+ return pdfManager.ensureCatalog("optionalContentConfig");
414
+ });
411
415
  handler.on("GetPermissions", function (data) {
412
416
  return pdfManager.ensureCatalog("permissions");
413
417
  });
@@ -421,7 +425,7 @@ var WorkerMessageHandler = {
421
425
  });
422
426
  });
423
427
  handler.on("GetStats", function wphSetupGetStats(data) {
424
- return pdfManager.pdfDocument.xref.stats;
428
+ return pdfManager.ensureXRef("stats");
425
429
  });
426
430
  handler.on("GetAnnotations", function ({
427
431
  pageIndex,
@@ -431,6 +435,65 @@ var WorkerMessageHandler = {
431
435
  return page.getAnnotationsData(intent);
432
436
  });
433
437
  });
438
+ handler.on("SaveDocument", function ({
439
+ numPages,
440
+ annotationStorage,
441
+ filename
442
+ }) {
443
+ pdfManager.requestLoadedStream();
444
+ const promises = [pdfManager.onLoadedStream()];
445
+ const document = pdfManager.pdfDocument;
446
+
447
+ for (let pageIndex = 0; pageIndex < numPages; pageIndex++) {
448
+ promises.push(pdfManager.getPage(pageIndex).then(function (page) {
449
+ const task = new WorkerTask(`Save: page ${pageIndex}`);
450
+ return page.save(handler, task, annotationStorage);
451
+ }));
452
+ }
453
+
454
+ return Promise.all(promises).then(([stream, ...refs]) => {
455
+ let newRefs = [];
456
+
457
+ for (const ref of refs) {
458
+ newRefs = ref.filter(x => x !== null).reduce((a, b) => a.concat(b), newRefs);
459
+ }
460
+
461
+ if (newRefs.length === 0) {
462
+ return stream.bytes;
463
+ }
464
+
465
+ const xref = document.xref;
466
+ let newXrefInfo = Object.create(null);
467
+
468
+ if (xref.trailer) {
469
+ const _info = Object.create(null);
470
+
471
+ const xrefInfo = xref.trailer.get("Info") || null;
472
+
473
+ if (xrefInfo) {
474
+ xrefInfo.forEach((key, value) => {
475
+ if ((0, _util.isString)(key) && (0, _util.isString)(value)) {
476
+ _info[key] = (0, _util.stringToPDFString)(value);
477
+ }
478
+ });
479
+ }
480
+
481
+ newXrefInfo = {
482
+ rootRef: xref.trailer.getRaw("Root") || null,
483
+ encrypt: xref.trailer.getRaw("Encrypt") || null,
484
+ newRef: xref.getNewRef(),
485
+ infoRef: xref.trailer.getRaw("Info") || null,
486
+ info: _info,
487
+ fileIds: xref.trailer.getRaw("ID") || null,
488
+ startXRef: document.startXRef,
489
+ filename
490
+ };
491
+ }
492
+
493
+ xref.resetNewRef();
494
+ return (0, _writer.incrementalUpdate)(stream.bytes, newXrefInfo, newRefs);
495
+ });
496
+ });
434
497
  handler.on("GetOperatorList", function wphSetupRenderPage(data, sink) {
435
498
  var pageIndex = data.pageIndex;
436
499
  pdfManager.getPage(pageIndex).then(function (page) {
@@ -442,7 +505,8 @@ var WorkerMessageHandler = {
442
505
  sink,
443
506
  task,
444
507
  intent: data.intent,
445
- renderInteractiveForms: data.renderInteractiveForms
508
+ renderInteractiveForms: data.renderInteractiveForms,
509
+ annotationStorage: data.annotationStorage
446
510
  }).then(function (operatorListInfo) {
447
511
  finishWorkerTask(task);
448
512
 
@@ -538,15 +602,16 @@ var WorkerMessageHandler = {
538
602
  docParams = null;
539
603
  });
540
604
  return workerHandlerName;
541
- },
605
+ }
542
606
 
543
- initializeFromPort(port) {
607
+ static initializeFromPort(port) {
544
608
  var handler = new _message_handler.MessageHandler("worker", "main", port);
545
609
  WorkerMessageHandler.setup(handler, port);
546
610
  handler.send("ready", null);
547
611
  }
548
612
 
549
- };
613
+ }
614
+
550
615
  exports.WorkerMessageHandler = WorkerMessageHandler;
551
616
 
552
617
  function isMessagePort(maybePort) {