pdfjs-dist 2.1.266 → 2.5.207

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 (191) hide show
  1. package/CODE_OF_CONDUCT.md +15 -0
  2. package/README.md +4 -0
  3. package/bower.json +1 -1
  4. package/build/pdf.js +8382 -18492
  5. package/build/pdf.js.map +1 -1
  6. package/build/pdf.min.js +22 -1
  7. package/build/pdf.worker.entry.js +5 -3
  8. package/build/pdf.worker.js +20417 -29816
  9. package/build/pdf.worker.js.map +1 -1
  10. package/build/pdf.worker.min.js +22 -1
  11. package/es5/build/pdf.js +25688 -0
  12. package/es5/build/pdf.js.map +1 -0
  13. package/{lib/shared/global_scope.js → es5/build/pdf.worker.entry.js} +5 -10
  14. package/es5/build/pdf.worker.js +58239 -0
  15. package/es5/build/pdf.worker.js.map +1 -0
  16. package/es5/web/images/annotation-check.svg +11 -0
  17. package/es5/web/images/annotation-comment.svg +16 -0
  18. package/es5/web/images/annotation-help.svg +26 -0
  19. package/es5/web/images/annotation-insert.svg +10 -0
  20. package/es5/web/images/annotation-key.svg +11 -0
  21. package/es5/web/images/annotation-newparagraph.svg +11 -0
  22. package/es5/web/images/annotation-noicon.svg +7 -0
  23. package/es5/web/images/annotation-note.svg +42 -0
  24. package/es5/web/images/annotation-paragraph.svg +16 -0
  25. package/es5/web/images/loading-icon.gif +0 -0
  26. package/es5/web/images/shadow.png +0 -0
  27. package/es5/web/images/texture.png +0 -0
  28. package/es5/web/pdf_viewer.css +403 -0
  29. package/es5/web/pdf_viewer.js +7742 -0
  30. package/es5/web/pdf_viewer.js.map +1 -0
  31. package/image_decoders/pdf.image_decoders.js +1475 -4897
  32. package/image_decoders/pdf.image_decoders.js.map +1 -1
  33. package/image_decoders/pdf.image_decoders.min.js +22 -1
  34. package/lib/README.md +7 -0
  35. package/lib/core/annotation.js +750 -899
  36. package/lib/core/arithmetic_decoder.js +81 -97
  37. package/lib/core/bidi.js +54 -46
  38. package/lib/core/ccitt.js +88 -81
  39. package/lib/core/ccitt_stream.js +15 -14
  40. package/lib/core/cff_parser.js +235 -183
  41. package/lib/core/charsets.js +4 -4
  42. package/lib/core/chunked_stream.js +447 -542
  43. package/lib/core/cmap.js +222 -264
  44. package/lib/core/colorspace.js +699 -863
  45. package/lib/core/core_utils.js +152 -0
  46. package/lib/core/crypto.js +379 -437
  47. package/lib/core/document.js +573 -660
  48. package/lib/core/encodings.js +15 -15
  49. package/lib/core/evaluator.js +1103 -868
  50. package/lib/core/font_renderer.js +135 -178
  51. package/lib/core/fonts.js +570 -491
  52. package/lib/core/function.js +291 -288
  53. package/lib/core/glyphlist.js +4527 -4526
  54. package/lib/core/image.js +145 -149
  55. package/lib/core/image_utils.js +170 -0
  56. package/lib/core/jbig2.js +325 -316
  57. package/lib/core/jbig2_stream.js +18 -17
  58. package/lib/core/jpeg_stream.js +21 -26
  59. package/lib/core/jpg.js +284 -232
  60. package/lib/core/jpx.js +161 -143
  61. package/lib/core/jpx_stream.js +28 -28
  62. package/lib/core/metrics.js +2929 -2929
  63. package/lib/core/murmurhash3.js +90 -101
  64. package/lib/core/obj.js +1183 -1157
  65. package/lib/core/operator_list.js +99 -67
  66. package/lib/core/parser.js +972 -911
  67. package/lib/core/pattern.js +87 -70
  68. package/lib/core/pdf_manager.js +150 -315
  69. package/lib/core/primitives.js +83 -56
  70. package/lib/core/ps_parser.js +175 -214
  71. package/lib/core/standard_fonts.js +237 -236
  72. package/lib/core/stream.js +94 -74
  73. package/lib/core/type1_parser.js +87 -69
  74. package/lib/core/unicode.js +1654 -1654
  75. package/lib/core/worker.js +193 -390
  76. package/lib/core/worker_stream.js +168 -0
  77. package/lib/display/annotation_layer.js +741 -972
  78. package/lib/display/api.js +1500 -1791
  79. package/lib/display/api_compatibility.js +12 -17
  80. package/lib/display/canvas.js +165 -165
  81. package/lib/display/content_disposition.js +40 -59
  82. package/lib/display/display_utils.js +515 -0
  83. package/lib/display/fetch_stream.js +183 -298
  84. package/lib/display/font_loader.js +273 -413
  85. package/lib/display/metadata.js +86 -98
  86. package/lib/display/network.js +266 -359
  87. package/lib/display/network_utils.js +25 -18
  88. package/lib/display/node_stream.js +285 -458
  89. package/lib/display/pattern_helper.js +113 -65
  90. package/lib/display/svg.js +1166 -901
  91. package/lib/display/text_layer.js +156 -132
  92. package/lib/display/transport_stream.js +262 -278
  93. package/lib/display/webgl.js +70 -83
  94. package/lib/display/worker_options.js +3 -3
  95. package/lib/display/xml_parser.js +303 -392
  96. package/lib/examples/node/domstubs.js +40 -37
  97. package/lib/pdf.js +226 -59
  98. package/lib/pdf.worker.js +14 -6
  99. package/lib/shared/compatibility.js +3 -246
  100. package/lib/shared/is_node.js +7 -6
  101. package/lib/shared/message_handler.js +327 -332
  102. package/lib/shared/util.js +266 -416
  103. package/lib/test/unit/annotation_spec.js +1555 -701
  104. package/lib/test/unit/api_spec.js +802 -604
  105. package/lib/test/unit/bidi_spec.js +7 -7
  106. package/lib/test/unit/cff_parser_spec.js +84 -69
  107. package/lib/test/unit/clitests_helper.js +7 -9
  108. package/lib/test/unit/cmap_spec.js +74 -76
  109. package/lib/test/unit/colorspace_spec.js +166 -161
  110. package/lib/test/unit/core_utils_spec.js +211 -0
  111. package/lib/test/unit/crypto_spec.js +181 -181
  112. package/lib/test/unit/custom_spec.js +20 -22
  113. package/lib/test/unit/display_svg_spec.js +34 -39
  114. package/lib/test/unit/display_utils_spec.js +263 -0
  115. package/lib/test/unit/document_spec.js +16 -21
  116. package/lib/test/unit/encodings_spec.js +12 -34
  117. package/lib/test/unit/evaluator_spec.js +83 -83
  118. package/lib/test/unit/fetch_stream_spec.js +111 -0
  119. package/lib/test/unit/function_spec.js +206 -204
  120. package/lib/test/unit/jasmine-boot.js +46 -30
  121. package/lib/test/unit/message_handler_spec.js +173 -159
  122. package/lib/test/unit/metadata_spec.js +69 -69
  123. package/lib/test/unit/murmurhash3_spec.js +12 -12
  124. package/lib/test/unit/network_spec.js +13 -61
  125. package/lib/test/unit/network_utils_spec.js +183 -119
  126. package/lib/test/unit/node_stream_spec.js +78 -92
  127. package/lib/test/unit/parser_spec.js +172 -114
  128. package/lib/test/unit/pdf_find_controller_spec.js +55 -86
  129. package/lib/test/unit/pdf_find_utils_spec.js +32 -32
  130. package/lib/test/unit/pdf_history_spec.js +32 -32
  131. package/lib/test/unit/primitives_spec.js +140 -125
  132. package/lib/test/unit/stream_spec.js +16 -14
  133. package/lib/test/unit/test_utils.js +131 -143
  134. package/lib/test/unit/testreporter.js +19 -19
  135. package/lib/test/unit/type1_parser_spec.js +42 -42
  136. package/lib/test/unit/ui_utils_spec.js +297 -459
  137. package/lib/test/unit/unicode_spec.js +38 -38
  138. package/lib/test/unit/util_spec.js +121 -305
  139. package/lib/web/annotation_layer_builder.js +66 -103
  140. package/lib/web/app.js +1328 -1214
  141. package/lib/web/app_options.js +105 -107
  142. package/lib/web/base_viewer.js +824 -838
  143. package/lib/web/chromecom.js +165 -252
  144. package/lib/web/debugger.js +149 -205
  145. package/lib/web/download_manager.js +39 -55
  146. package/lib/web/firefox_print_service.js +37 -27
  147. package/lib/web/firefoxcom.js +212 -363
  148. package/lib/web/genericcom.js +26 -108
  149. package/lib/web/genericl10n.js +24 -153
  150. package/lib/web/grab_to_pan.js +32 -30
  151. package/lib/web/interfaces.js +80 -254
  152. package/lib/web/overlay_manager.js +70 -246
  153. package/lib/web/password_prompt.js +38 -64
  154. package/lib/web/pdf_attachment_viewer.js +113 -131
  155. package/lib/web/pdf_cursor_tools.js +75 -102
  156. package/lib/web/pdf_document_properties.js +221 -306
  157. package/lib/web/pdf_find_bar.js +136 -170
  158. package/lib/web/pdf_find_controller.js +491 -548
  159. package/lib/web/pdf_find_utils.js +13 -13
  160. package/lib/web/pdf_history.js +397 -406
  161. package/lib/web/pdf_link_service.js +304 -348
  162. package/lib/web/pdf_outline_viewer.js +140 -175
  163. package/lib/web/pdf_page_view.js +452 -523
  164. package/lib/web/pdf_presentation_mode.js +308 -357
  165. package/lib/web/pdf_print_service.js +90 -104
  166. package/lib/web/pdf_rendering_queue.js +84 -108
  167. package/lib/web/pdf_sidebar.js +276 -306
  168. package/lib/web/pdf_sidebar_resizer.js +92 -119
  169. package/lib/web/pdf_single_page_viewer.js +77 -126
  170. package/lib/web/pdf_thumbnail_view.js +276 -297
  171. package/lib/web/pdf_thumbnail_viewer.js +186 -206
  172. package/lib/web/pdf_viewer.component.js +20 -21
  173. package/lib/web/pdf_viewer.js +55 -115
  174. package/lib/web/preferences.js +82 -286
  175. package/lib/web/secondary_toolbar.js +164 -196
  176. package/lib/web/text_layer_builder.js +282 -339
  177. package/lib/web/toolbar.js +217 -210
  178. package/lib/web/ui_utils.js +267 -420
  179. package/lib/web/view_history.js +52 -226
  180. package/lib/web/viewer_compatibility.js +21 -6
  181. package/package.json +2 -9
  182. package/web/pdf_viewer.css +35 -25
  183. package/web/pdf_viewer.js +3489 -4855
  184. package/web/pdf_viewer.js.map +1 -1
  185. package/webpack.js +14 -5
  186. package/external/streams/streams-lib.js +0 -3962
  187. package/external/url/url-lib.js +0 -627
  188. package/lib/display/dom_utils.js +0 -494
  189. package/lib/shared/streams_polyfill.js +0 -43
  190. package/lib/shared/url_polyfill.js +0 -56
  191. package/lib/test/unit/dom_utils_spec.js +0 -89
@@ -2,7 +2,7 @@
2
2
  * @licstart The following is the entire license notice for the
3
3
  * Javascript code in this page
4
4
  *
5
- * Copyright 2018 Mozilla Foundation
5
+ * Copyright 2020 Mozilla Foundation
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
8
8
  * you may not use this file except in compliance with the License.
@@ -26,62 +26,47 @@ Object.defineProperty(exports, "__esModule", {
26
26
  });
27
27
  exports.PDFDocument = exports.Page = void 0;
28
28
 
29
- var _util = require("../shared/util");
29
+ var _util = require("../shared/util.js");
30
30
 
31
- var _obj = require("./obj");
31
+ var _obj = require("./obj.js");
32
32
 
33
- var _primitives = require("./primitives");
33
+ var _primitives = require("./primitives.js");
34
34
 
35
- var _stream2 = require("./stream");
35
+ var _core_utils = require("./core_utils.js");
36
36
 
37
- var _annotation = require("./annotation");
37
+ var _stream = require("./stream.js");
38
38
 
39
- var _crypto = require("./crypto");
39
+ var _annotation = require("./annotation.js");
40
40
 
41
- var _parser = require("./parser");
41
+ var _crypto = require("./crypto.js");
42
42
 
43
- var _operator_list = require("./operator_list");
43
+ var _parser = require("./parser.js");
44
44
 
45
- var _evaluator = require("./evaluator");
45
+ var _operator_list = require("./operator_list.js");
46
46
 
47
- var _function = require("./function");
47
+ var _evaluator = require("./evaluator.js");
48
48
 
49
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
49
+ var _function = require("./function.js");
50
50
 
51
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
52
-
53
- function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
54
-
55
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
56
-
57
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
58
-
59
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
60
-
61
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
62
-
63
- var DEFAULT_USER_UNIT = 1.0;
64
- var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
51
+ const DEFAULT_USER_UNIT = 1.0;
52
+ const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
65
53
 
66
54
  function isAnnotationRenderable(annotation, intent) {
67
- return intent === 'display' && annotation.viewable || intent === 'print' && annotation.printable;
55
+ return intent === "display" && annotation.viewable || intent === "print" && annotation.printable;
68
56
  }
69
57
 
70
- var Page =
71
- /*#__PURE__*/
72
- function () {
73
- function Page(_ref) {
74
- var pdfManager = _ref.pdfManager,
75
- xref = _ref.xref,
76
- pageIndex = _ref.pageIndex,
77
- pageDict = _ref.pageDict,
78
- ref = _ref.ref,
79
- fontCache = _ref.fontCache,
80
- builtInCMapCache = _ref.builtInCMapCache,
81
- pdfFunctionFactory = _ref.pdfFunctionFactory;
82
-
83
- _classCallCheck(this, Page);
84
-
58
+ class Page {
59
+ constructor({
60
+ pdfManager,
61
+ xref,
62
+ pageIndex,
63
+ pageDict,
64
+ ref,
65
+ fontCache,
66
+ builtInCMapCache,
67
+ globalImageCache,
68
+ pdfFunctionFactory
69
+ }) {
85
70
  this.pdfManager = pdfManager;
86
71
  this.pageIndex = pageIndex;
87
72
  this.pageDict = pageDict;
@@ -89,416 +74,367 @@ function () {
89
74
  this.ref = ref;
90
75
  this.fontCache = fontCache;
91
76
  this.builtInCMapCache = builtInCMapCache;
77
+ this.globalImageCache = globalImageCache;
92
78
  this.pdfFunctionFactory = pdfFunctionFactory;
93
79
  this.evaluatorOptions = pdfManager.evaluatorOptions;
94
80
  this.resourcesPromise = null;
95
- var uniquePrefix = "p".concat(this.pageIndex, "_");
96
- var idCounters = {
81
+ const idCounters = {
97
82
  obj: 0
98
83
  };
99
84
  this.idFactory = {
100
- createObjId: function createObjId() {
101
- return uniquePrefix + ++idCounters.obj;
85
+ createObjId() {
86
+ return `p${pageIndex}_${++idCounters.obj}`;
87
+ },
88
+
89
+ getDocId() {
90
+ return `g_${pdfManager.docId}`;
102
91
  }
92
+
103
93
  };
104
94
  }
105
95
 
106
- _createClass(Page, [{
107
- key: "_getInheritableProperty",
108
- value: function _getInheritableProperty(key) {
109
- var getArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
110
- var value = (0, _util.getInheritableProperty)({
111
- dict: this.pageDict,
112
- key: key,
113
- getArray: getArray,
114
- stopWhenFound: false
115
- });
116
-
117
- if (!Array.isArray(value)) {
118
- return value;
119
- }
96
+ _getInheritableProperty(key, getArray = false) {
97
+ const value = (0, _core_utils.getInheritableProperty)({
98
+ dict: this.pageDict,
99
+ key,
100
+ getArray,
101
+ stopWhenFound: false
102
+ });
120
103
 
121
- if (value.length === 1 || !(0, _primitives.isDict)(value[0])) {
122
- return value[0];
123
- }
104
+ if (!Array.isArray(value)) {
105
+ return value;
106
+ }
124
107
 
125
- return _primitives.Dict.merge(this.xref, value);
108
+ if (value.length === 1 || !(0, _primitives.isDict)(value[0])) {
109
+ return value[0];
126
110
  }
127
- }, {
128
- key: "getContentStream",
129
- value: function getContentStream() {
130
- var content = this.content;
131
- var stream;
132
-
133
- if (Array.isArray(content)) {
134
- var xref = this.xref;
135
- var streams = [];
136
- var _iteratorNormalCompletion = true;
137
- var _didIteratorError = false;
138
- var _iteratorError = undefined;
139
-
140
- try {
141
- for (var _iterator = content[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
142
- var _stream = _step.value;
143
- streams.push(xref.fetchIfRef(_stream));
144
- }
145
- } catch (err) {
146
- _didIteratorError = true;
147
- _iteratorError = err;
148
- } finally {
149
- try {
150
- if (!_iteratorNormalCompletion && _iterator.return != null) {
151
- _iterator.return();
152
- }
153
- } finally {
154
- if (_didIteratorError) {
155
- throw _iteratorError;
156
- }
157
- }
158
- }
159
111
 
160
- stream = new _stream2.StreamsSequenceStream(streams);
161
- } else if ((0, _primitives.isStream)(content)) {
162
- stream = content;
163
- } else {
164
- stream = new _stream2.NullStream();
165
- }
112
+ return _primitives.Dict.merge(this.xref, value);
113
+ }
166
114
 
167
- return stream;
168
- }
169
- }, {
170
- key: "loadResources",
171
- value: function loadResources(keys) {
172
- var _this = this;
115
+ get content() {
116
+ return this.pageDict.get("Contents");
117
+ }
118
+
119
+ get resources() {
120
+ return (0, _util.shadow)(this, "resources", this._getInheritableProperty("Resources") || _primitives.Dict.empty);
121
+ }
173
122
 
174
- if (!this.resourcesPromise) {
175
- this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
123
+ _getBoundingBox(name) {
124
+ const box = this._getInheritableProperty(name, true);
125
+
126
+ if (Array.isArray(box) && box.length === 4) {
127
+ if (box[2] - box[0] !== 0 && box[3] - box[1] !== 0) {
128
+ return box;
176
129
  }
177
130
 
178
- return this.resourcesPromise.then(function () {
179
- var objectLoader = new _obj.ObjectLoader(_this.resources, keys, _this.xref);
180
- return objectLoader.load();
181
- });
131
+ (0, _util.warn)(`Empty /${name} entry.`);
182
132
  }
183
- }, {
184
- key: "getOperatorList",
185
- value: function getOperatorList(_ref2) {
186
- var _this2 = this;
187
-
188
- var handler = _ref2.handler,
189
- task = _ref2.task,
190
- intent = _ref2.intent,
191
- renderInteractiveForms = _ref2.renderInteractiveForms;
192
- var contentStreamPromise = this.pdfManager.ensure(this, 'getContentStream');
193
- var resourcesPromise = this.loadResources(['ExtGState', 'ColorSpace', 'Pattern', 'Shading', 'XObject', 'Font']);
194
- var partialEvaluator = new _evaluator.PartialEvaluator({
195
- pdfManager: this.pdfManager,
196
- xref: this.xref,
197
- handler: handler,
198
- pageIndex: this.pageIndex,
199
- idFactory: this.idFactory,
200
- fontCache: this.fontCache,
201
- builtInCMapCache: this.builtInCMapCache,
202
- options: this.evaluatorOptions,
203
- pdfFunctionFactory: this.pdfFunctionFactory
204
- });
205
- var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
206
- var pageListPromise = dataPromises.then(function (_ref3) {
207
- var _ref4 = _slicedToArray(_ref3, 1),
208
- contentStream = _ref4[0];
209
-
210
- var opList = new _operator_list.OperatorList(intent, handler, _this2.pageIndex);
211
- handler.send('StartRenderPage', {
212
- transparency: partialEvaluator.hasBlendModes(_this2.resources),
213
- pageIndex: _this2.pageIndex,
214
- intent: intent
215
- });
216
- return partialEvaluator.getOperatorList({
217
- stream: contentStream,
218
- task: task,
219
- resources: _this2.resources,
220
- operatorList: opList
221
- }).then(function () {
222
- return opList;
223
- });
224
- });
225
- return Promise.all([pageListPromise, this._parsedAnnotations]).then(function (_ref5) {
226
- var _ref6 = _slicedToArray(_ref5, 2),
227
- pageOpList = _ref6[0],
228
- annotations = _ref6[1];
229
-
230
- if (annotations.length === 0) {
231
- pageOpList.flush(true);
232
- return pageOpList;
233
- }
234
133
 
235
- var opListPromises = [];
236
- var _iteratorNormalCompletion2 = true;
237
- var _didIteratorError2 = false;
238
- var _iteratorError2 = undefined;
134
+ return null;
135
+ }
239
136
 
240
- try {
241
- for (var _iterator2 = annotations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
242
- var annotation = _step2.value;
137
+ get mediaBox() {
138
+ return (0, _util.shadow)(this, "mediaBox", this._getBoundingBox("MediaBox") || LETTER_SIZE_MEDIABOX);
139
+ }
243
140
 
244
- if (isAnnotationRenderable(annotation, intent)) {
245
- opListPromises.push(annotation.getOperatorList(partialEvaluator, task, renderInteractiveForms));
246
- }
247
- }
248
- } catch (err) {
249
- _didIteratorError2 = true;
250
- _iteratorError2 = err;
251
- } finally {
252
- try {
253
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
254
- _iterator2.return();
255
- }
256
- } finally {
257
- if (_didIteratorError2) {
258
- throw _iteratorError2;
259
- }
260
- }
261
- }
141
+ get cropBox() {
142
+ return (0, _util.shadow)(this, "cropBox", this._getBoundingBox("CropBox") || this.mediaBox);
143
+ }
262
144
 
263
- return Promise.all(opListPromises).then(function (opLists) {
264
- pageOpList.addOp(_util.OPS.beginAnnotations, []);
265
- var _iteratorNormalCompletion3 = true;
266
- var _didIteratorError3 = false;
267
- var _iteratorError3 = undefined;
268
-
269
- try {
270
- for (var _iterator3 = opLists[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
271
- var opList = _step3.value;
272
- pageOpList.addOpList(opList);
273
- }
274
- } catch (err) {
275
- _didIteratorError3 = true;
276
- _iteratorError3 = err;
277
- } finally {
278
- try {
279
- if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
280
- _iterator3.return();
281
- }
282
- } finally {
283
- if (_didIteratorError3) {
284
- throw _iteratorError3;
285
- }
286
- }
287
- }
145
+ get userUnit() {
146
+ let obj = this.pageDict.get("UserUnit");
288
147
 
289
- pageOpList.addOp(_util.OPS.endAnnotations, []);
290
- pageOpList.flush(true);
291
- return pageOpList;
292
- });
293
- });
148
+ if (!(0, _util.isNum)(obj) || obj <= 0) {
149
+ obj = DEFAULT_USER_UNIT;
294
150
  }
295
- }, {
296
- key: "extractTextContent",
297
- value: function extractTextContent(_ref7) {
298
- var _this3 = this;
299
-
300
- var handler = _ref7.handler,
301
- task = _ref7.task,
302
- normalizeWhitespace = _ref7.normalizeWhitespace,
303
- sink = _ref7.sink,
304
- combineTextItems = _ref7.combineTextItems;
305
- var contentStreamPromise = this.pdfManager.ensure(this, 'getContentStream');
306
- var resourcesPromise = this.loadResources(['ExtGState', 'XObject', 'Font']);
307
- var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
308
- return dataPromises.then(function (_ref8) {
309
- var _ref9 = _slicedToArray(_ref8, 1),
310
- contentStream = _ref9[0];
311
-
312
- var partialEvaluator = new _evaluator.PartialEvaluator({
313
- pdfManager: _this3.pdfManager,
314
- xref: _this3.xref,
315
- handler: handler,
316
- pageIndex: _this3.pageIndex,
317
- idFactory: _this3.idFactory,
318
- fontCache: _this3.fontCache,
319
- builtInCMapCache: _this3.builtInCMapCache,
320
- options: _this3.evaluatorOptions,
321
- pdfFunctionFactory: _this3.pdfFunctionFactory
322
- });
323
- return partialEvaluator.getTextContent({
324
- stream: contentStream,
325
- task: task,
326
- resources: _this3.resources,
327
- normalizeWhitespace: normalizeWhitespace,
328
- combineTextItems: combineTextItems,
329
- sink: sink
330
- });
331
- });
332
- }
333
- }, {
334
- key: "getAnnotationsData",
335
- value: function getAnnotationsData(intent) {
336
- return this._parsedAnnotations.then(function (annotations) {
337
- var annotationsData = [];
338
-
339
- for (var i = 0, ii = annotations.length; i < ii; i++) {
340
- if (!intent || isAnnotationRenderable(annotations[i], intent)) {
341
- annotationsData.push(annotations[i].data);
342
- }
343
- }
344
151
 
345
- return annotationsData;
346
- });
347
- }
348
- }, {
349
- key: "content",
350
- get: function get() {
351
- return this.pageDict.get('Contents');
352
- }
353
- }, {
354
- key: "resources",
355
- get: function get() {
356
- return (0, _util.shadow)(this, 'resources', this._getInheritableProperty('Resources') || _primitives.Dict.empty);
357
- }
358
- }, {
359
- key: "mediaBox",
360
- get: function get() {
361
- var mediaBox = this._getInheritableProperty('MediaBox', true);
152
+ return (0, _util.shadow)(this, "userUnit", obj);
153
+ }
362
154
 
363
- if (!Array.isArray(mediaBox) || mediaBox.length !== 4) {
364
- return (0, _util.shadow)(this, 'mediaBox', LETTER_SIZE_MEDIABOX);
365
- }
155
+ get view() {
156
+ const {
157
+ cropBox,
158
+ mediaBox
159
+ } = this;
160
+ let view;
366
161
 
367
- return (0, _util.shadow)(this, 'mediaBox', mediaBox);
368
- }
369
- }, {
370
- key: "cropBox",
371
- get: function get() {
372
- var cropBox = this._getInheritableProperty('CropBox', true);
162
+ if (cropBox === mediaBox || (0, _util.isArrayEqual)(cropBox, mediaBox)) {
163
+ view = mediaBox;
164
+ } else {
165
+ const box = _util.Util.intersect(cropBox, mediaBox);
373
166
 
374
- if (!Array.isArray(cropBox) || cropBox.length !== 4) {
375
- return (0, _util.shadow)(this, 'cropBox', this.mediaBox);
167
+ if (box && box[2] - box[0] !== 0 && box[3] - box[1] !== 0) {
168
+ view = box;
169
+ } else {
170
+ (0, _util.warn)("Empty /CropBox and /MediaBox intersection.");
376
171
  }
172
+ }
173
+
174
+ return (0, _util.shadow)(this, "view", view || mediaBox);
175
+ }
176
+
177
+ get rotate() {
178
+ let rotate = this._getInheritableProperty("Rotate") || 0;
377
179
 
378
- return (0, _util.shadow)(this, 'cropBox', cropBox);
180
+ if (rotate % 90 !== 0) {
181
+ rotate = 0;
182
+ } else if (rotate >= 360) {
183
+ rotate = rotate % 360;
184
+ } else if (rotate < 0) {
185
+ rotate = (rotate % 360 + 360) % 360;
379
186
  }
380
- }, {
381
- key: "userUnit",
382
- get: function get() {
383
- var obj = this.pageDict.get('UserUnit');
384
187
 
385
- if (!(0, _util.isNum)(obj) || obj <= 0) {
386
- obj = DEFAULT_USER_UNIT;
188
+ return (0, _util.shadow)(this, "rotate", rotate);
189
+ }
190
+
191
+ getContentStream() {
192
+ const content = this.content;
193
+ let stream;
194
+
195
+ if (Array.isArray(content)) {
196
+ const xref = this.xref;
197
+ const streams = [];
198
+
199
+ for (const subStream of content) {
200
+ streams.push(xref.fetchIfRef(subStream));
387
201
  }
388
202
 
389
- return (0, _util.shadow)(this, 'userUnit', obj);
203
+ stream = new _stream.StreamsSequenceStream(streams);
204
+ } else if ((0, _primitives.isStream)(content)) {
205
+ stream = content;
206
+ } else {
207
+ stream = new _stream.NullStream();
390
208
  }
391
- }, {
392
- key: "view",
393
- get: function get() {
394
- var mediaBox = this.mediaBox,
395
- cropBox = this.cropBox;
396
-
397
- if (mediaBox === cropBox) {
398
- return (0, _util.shadow)(this, 'view', mediaBox);
399
- }
400
209
 
401
- var intersection = _util.Util.intersect(cropBox, mediaBox);
210
+ return stream;
211
+ }
402
212
 
403
- return (0, _util.shadow)(this, 'view', intersection || mediaBox);
213
+ loadResources(keys) {
214
+ if (!this.resourcesPromise) {
215
+ this.resourcesPromise = this.pdfManager.ensure(this, "resources");
404
216
  }
405
- }, {
406
- key: "rotate",
407
- get: function get() {
408
- var rotate = this._getInheritableProperty('Rotate') || 0;
409
-
410
- if (rotate % 90 !== 0) {
411
- rotate = 0;
412
- } else if (rotate >= 360) {
413
- rotate = rotate % 360;
414
- } else if (rotate < 0) {
415
- rotate = (rotate % 360 + 360) % 360;
217
+
218
+ return this.resourcesPromise.then(() => {
219
+ const objectLoader = new _obj.ObjectLoader(this.resources, keys, this.xref);
220
+ return objectLoader.load();
221
+ });
222
+ }
223
+
224
+ getOperatorList({
225
+ handler,
226
+ sink,
227
+ task,
228
+ intent,
229
+ renderInteractiveForms
230
+ }) {
231
+ const contentStreamPromise = this.pdfManager.ensure(this, "getContentStream");
232
+ const resourcesPromise = this.loadResources(["ExtGState", "ColorSpace", "Pattern", "Shading", "XObject", "Font"]);
233
+ const partialEvaluator = new _evaluator.PartialEvaluator({
234
+ xref: this.xref,
235
+ handler,
236
+ pageIndex: this.pageIndex,
237
+ idFactory: this.idFactory,
238
+ fontCache: this.fontCache,
239
+ builtInCMapCache: this.builtInCMapCache,
240
+ globalImageCache: this.globalImageCache,
241
+ options: this.evaluatorOptions,
242
+ pdfFunctionFactory: this.pdfFunctionFactory
243
+ });
244
+ const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
245
+ const pageListPromise = dataPromises.then(([contentStream]) => {
246
+ const opList = new _operator_list.OperatorList(intent, sink, this.pageIndex);
247
+ handler.send("StartRenderPage", {
248
+ transparency: partialEvaluator.hasBlendModes(this.resources),
249
+ pageIndex: this.pageIndex,
250
+ intent
251
+ });
252
+ return partialEvaluator.getOperatorList({
253
+ stream: contentStream,
254
+ task,
255
+ resources: this.resources,
256
+ operatorList: opList
257
+ }).then(function () {
258
+ return opList;
259
+ });
260
+ });
261
+ return Promise.all([pageListPromise, this._parsedAnnotations]).then(function ([pageOpList, annotations]) {
262
+ if (annotations.length === 0) {
263
+ pageOpList.flush(true);
264
+ return {
265
+ length: pageOpList.totalLength
266
+ };
416
267
  }
417
268
 
418
- return (0, _util.shadow)(this, 'rotate', rotate);
419
- }
420
- }, {
421
- key: "annotations",
422
- get: function get() {
423
- return (0, _util.shadow)(this, 'annotations', this._getInheritableProperty('Annots') || []);
424
- }
425
- }, {
426
- key: "_parsedAnnotations",
427
- get: function get() {
428
- var _this4 = this;
269
+ const opListPromises = [];
270
+
271
+ for (const annotation of annotations) {
272
+ if (isAnnotationRenderable(annotation, intent)) {
273
+ opListPromises.push(annotation.getOperatorList(partialEvaluator, task, renderInteractiveForms).catch(function (reason) {
274
+ (0, _util.warn)("getOperatorList - ignoring annotation data during " + `"${task.name}" task: "${reason}".`);
275
+ return null;
276
+ }));
277
+ }
278
+ }
429
279
 
430
- var parsedAnnotations = this.pdfManager.ensure(this, 'annotations').then(function () {
431
- var annotationRefs = _this4.annotations;
432
- var annotationPromises = [];
280
+ return Promise.all(opListPromises).then(function (opLists) {
281
+ pageOpList.addOp(_util.OPS.beginAnnotations, []);
433
282
 
434
- for (var i = 0, ii = annotationRefs.length; i < ii; i++) {
435
- annotationPromises.push(_annotation.AnnotationFactory.create(_this4.xref, annotationRefs[i], _this4.pdfManager, _this4.idFactory));
283
+ for (const opList of opLists) {
284
+ pageOpList.addOpList(opList);
436
285
  }
437
286
 
438
- return Promise.all(annotationPromises).then(function (annotations) {
439
- return annotations.filter(function isDefined(annotation) {
440
- return !!annotation;
441
- });
442
- }, function (reason) {
443
- (0, _util.warn)("_parsedAnnotations: \"".concat(reason, "\"."));
444
- return [];
445
- });
287
+ pageOpList.addOp(_util.OPS.endAnnotations, []);
288
+ pageOpList.flush(true);
289
+ return {
290
+ length: pageOpList.totalLength
291
+ };
446
292
  });
447
- return (0, _util.shadow)(this, '_parsedAnnotations', parsedAnnotations);
448
- }
449
- }]);
293
+ });
294
+ }
295
+
296
+ extractTextContent({
297
+ handler,
298
+ task,
299
+ normalizeWhitespace,
300
+ sink,
301
+ combineTextItems
302
+ }) {
303
+ const contentStreamPromise = this.pdfManager.ensure(this, "getContentStream");
304
+ const resourcesPromise = this.loadResources(["ExtGState", "XObject", "Font"]);
305
+ const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
306
+ return dataPromises.then(([contentStream]) => {
307
+ const partialEvaluator = new _evaluator.PartialEvaluator({
308
+ xref: this.xref,
309
+ handler,
310
+ pageIndex: this.pageIndex,
311
+ idFactory: this.idFactory,
312
+ fontCache: this.fontCache,
313
+ builtInCMapCache: this.builtInCMapCache,
314
+ globalImageCache: this.globalImageCache,
315
+ options: this.evaluatorOptions,
316
+ pdfFunctionFactory: this.pdfFunctionFactory
317
+ });
318
+ return partialEvaluator.getTextContent({
319
+ stream: contentStream,
320
+ task,
321
+ resources: this.resources,
322
+ normalizeWhitespace,
323
+ combineTextItems,
324
+ sink
325
+ });
326
+ });
327
+ }
450
328
 
451
- return Page;
452
- }();
329
+ getAnnotationsData(intent) {
330
+ return this._parsedAnnotations.then(function (annotations) {
331
+ const annotationsData = [];
453
332
 
454
- exports.Page = Page;
455
- var FINGERPRINT_FIRST_BYTES = 1024;
456
- var EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' + '\x00\x00\x00\x00\x00\x00\x00\x00\x00';
333
+ for (let i = 0, ii = annotations.length; i < ii; i++) {
334
+ if (!intent || isAnnotationRenderable(annotations[i], intent)) {
335
+ annotationsData.push(annotations[i].data);
336
+ }
337
+ }
457
338
 
458
- function find(stream, needle, limit, backwards) {
459
- var pos = stream.pos;
460
- var end = stream.end;
339
+ return annotationsData;
340
+ });
341
+ }
461
342
 
462
- if (pos + limit > end) {
463
- limit = end - pos;
343
+ get annotations() {
344
+ return (0, _util.shadow)(this, "annotations", this._getInheritableProperty("Annots") || []);
464
345
  }
465
346
 
466
- var strBuf = [];
347
+ get _parsedAnnotations() {
348
+ const parsedAnnotations = this.pdfManager.ensure(this, "annotations").then(() => {
349
+ const annotationPromises = [];
467
350
 
468
- for (var i = 0; i < limit; ++i) {
469
- strBuf.push(String.fromCharCode(stream.getByte()));
351
+ for (const annotationRef of this.annotations) {
352
+ annotationPromises.push(_annotation.AnnotationFactory.create(this.xref, annotationRef, this.pdfManager, this.idFactory).catch(function (reason) {
353
+ (0, _util.warn)(`_parsedAnnotations: "${reason}".`);
354
+ return null;
355
+ }));
356
+ }
357
+
358
+ return Promise.all(annotationPromises).then(function (annotations) {
359
+ return annotations.filter(annotation => !!annotation);
360
+ });
361
+ });
362
+ return (0, _util.shadow)(this, "_parsedAnnotations", parsedAnnotations);
470
363
  }
471
364
 
472
- var str = strBuf.join('');
473
- stream.pos = pos;
474
- var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
365
+ }
475
366
 
476
- if (index === -1) {
367
+ exports.Page = Page;
368
+ const PDF_HEADER_SIGNATURE = new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2d]);
369
+ const STARTXREF_SIGNATURE = new Uint8Array([0x73, 0x74, 0x61, 0x72, 0x74, 0x78, 0x72, 0x65, 0x66]);
370
+ const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6e, 0x64, 0x6f, 0x62, 0x6a]);
371
+ const FINGERPRINT_FIRST_BYTES = 1024;
372
+ const EMPTY_FINGERPRINT = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
373
+ const PDF_HEADER_VERSION_REGEXP = /^[1-9]\.[0-9]$/;
374
+
375
+ function find(stream, signature, limit = 1024, backwards = false) {
376
+ const signatureLength = signature.length;
377
+ const scanBytes = stream.peekBytes(limit);
378
+ const scanLength = scanBytes.length - signatureLength;
379
+
380
+ if (scanLength <= 0) {
477
381
  return false;
478
382
  }
479
383
 
480
- stream.pos += index;
481
- return true;
482
- }
384
+ if (backwards) {
385
+ const signatureEnd = signatureLength - 1;
386
+ let pos = scanBytes.length - 1;
483
387
 
484
- var PDFDocument =
485
- /*#__PURE__*/
486
- function () {
487
- function PDFDocument(pdfManager, arg) {
488
- _classCallCheck(this, PDFDocument);
388
+ while (pos >= signatureEnd) {
389
+ let j = 0;
489
390
 
490
- var stream;
391
+ while (j < signatureLength && scanBytes[pos - j] === signature[signatureEnd - j]) {
392
+ j++;
393
+ }
394
+
395
+ if (j >= signatureLength) {
396
+ stream.pos += pos - signatureEnd;
397
+ return true;
398
+ }
399
+
400
+ pos--;
401
+ }
402
+ } else {
403
+ let pos = 0;
404
+
405
+ while (pos <= scanLength) {
406
+ let j = 0;
407
+
408
+ while (j < signatureLength && scanBytes[pos + j] === signature[j]) {
409
+ j++;
410
+ }
411
+
412
+ if (j >= signatureLength) {
413
+ stream.pos += pos;
414
+ return true;
415
+ }
416
+
417
+ pos++;
418
+ }
419
+ }
420
+
421
+ return false;
422
+ }
423
+
424
+ class PDFDocument {
425
+ constructor(pdfManager, arg) {
426
+ let stream;
491
427
 
492
428
  if ((0, _primitives.isStream)(arg)) {
493
429
  stream = arg;
494
430
  } else if ((0, _util.isArrayBuffer)(arg)) {
495
- stream = new _stream2.Stream(arg);
431
+ stream = new _stream.Stream(arg);
496
432
  } else {
497
- throw new Error('PDFDocument: Unknown argument type');
433
+ throw new Error("PDFDocument: Unknown argument type");
498
434
  }
499
435
 
500
436
  if (stream.length <= 0) {
501
- throw new Error('PDFDocument: Stream must have data');
437
+ throw new _util.InvalidPDFException("The PDF file is empty, i.e. its size is zero bytes.");
502
438
  }
503
439
 
504
440
  this.pdfManager = pdfManager;
@@ -511,341 +447,318 @@ function () {
511
447
  this._pagePromises = [];
512
448
  }
513
449
 
514
- _createClass(PDFDocument, [{
515
- key: "parse",
516
- value: function parse(recoveryMode) {
517
- this.setup(recoveryMode);
518
- var version = this.catalog.catDict.get('Version');
450
+ parse(recoveryMode) {
451
+ this.setup(recoveryMode);
452
+ const version = this.catalog.catDict.get("Version");
519
453
 
520
- if ((0, _primitives.isName)(version)) {
521
- this.pdfFormatVersion = version.name;
522
- }
454
+ if ((0, _primitives.isName)(version)) {
455
+ this.pdfFormatVersion = version.name;
456
+ }
523
457
 
524
- try {
525
- this.acroForm = this.catalog.catDict.get('AcroForm');
458
+ try {
459
+ this.acroForm = this.catalog.catDict.get("AcroForm");
526
460
 
527
- if (this.acroForm) {
528
- this.xfa = this.acroForm.get('XFA');
529
- var fields = this.acroForm.get('Fields');
461
+ if (this.acroForm) {
462
+ this.xfa = this.acroForm.get("XFA");
463
+ const fields = this.acroForm.get("Fields");
530
464
 
531
- if ((!fields || !Array.isArray(fields) || fields.length === 0) && !this.xfa) {
532
- this.acroForm = null;
533
- }
534
- }
535
- } catch (ex) {
536
- if (ex instanceof _util.MissingDataException) {
537
- throw ex;
465
+ if ((!Array.isArray(fields) || fields.length === 0) && !this.xfa) {
466
+ this.acroForm = null;
538
467
  }
468
+ }
469
+ } catch (ex) {
470
+ if (ex instanceof _core_utils.MissingDataException) {
471
+ throw ex;
472
+ }
473
+
474
+ (0, _util.info)("Cannot fetch AcroForm entry; assuming no AcroForms are present");
475
+ this.acroForm = null;
476
+ }
477
+
478
+ try {
479
+ const collection = this.catalog.catDict.get("Collection");
480
+
481
+ if ((0, _primitives.isDict)(collection) && collection.getKeys().length > 0) {
482
+ this.collection = collection;
483
+ }
484
+ } catch (ex) {
485
+ if (ex instanceof _core_utils.MissingDataException) {
486
+ throw ex;
487
+ }
539
488
 
540
- (0, _util.info)('Cannot fetch AcroForm entry; assuming no AcroForms are present');
541
- this.acroForm = null;
489
+ (0, _util.info)("Cannot fetch Collection dictionary.");
490
+ }
491
+ }
492
+
493
+ get linearization() {
494
+ let linearization = null;
495
+
496
+ try {
497
+ linearization = _parser.Linearization.create(this.stream);
498
+ } catch (err) {
499
+ if (err instanceof _core_utils.MissingDataException) {
500
+ throw err;
542
501
  }
502
+
503
+ (0, _util.info)(err);
543
504
  }
544
- }, {
545
- key: "checkHeader",
546
- value: function checkHeader() {
547
- var stream = this.stream;
505
+
506
+ return (0, _util.shadow)(this, "linearization", linearization);
507
+ }
508
+
509
+ get startXRef() {
510
+ const stream = this.stream;
511
+ let startXRef = 0;
512
+
513
+ if (this.linearization) {
548
514
  stream.reset();
549
515
 
550
- if (!find(stream, '%PDF-', 1024)) {
551
- return;
516
+ if (find(stream, ENDOBJ_SIGNATURE)) {
517
+ startXRef = stream.pos + 6 - stream.start;
552
518
  }
519
+ } else {
520
+ const step = 1024;
521
+ const startXRefLength = STARTXREF_SIGNATURE.length;
522
+ let found = false,
523
+ pos = stream.end;
553
524
 
554
- stream.moveStart();
555
- var MAX_PDF_VERSION_LENGTH = 12;
556
- var version = '',
557
- ch;
525
+ while (!found && pos > 0) {
526
+ pos -= step - startXRefLength;
558
527
 
559
- while ((ch = stream.getByte()) > 0x20) {
560
- if (version.length >= MAX_PDF_VERSION_LENGTH) {
561
- break;
528
+ if (pos < 0) {
529
+ pos = 0;
562
530
  }
563
531
 
564
- version += String.fromCharCode(ch);
532
+ stream.pos = pos;
533
+ found = find(stream, STARTXREF_SIGNATURE, step, true);
565
534
  }
566
535
 
567
- if (!this.pdfFormatVersion) {
568
- this.pdfFormatVersion = version.substring(5);
569
- }
570
- }
571
- }, {
572
- key: "parseStartXRef",
573
- value: function parseStartXRef() {
574
- this.xref.setStartXRef(this.startXRef);
575
- }
576
- }, {
577
- key: "setup",
578
- value: function setup(recoveryMode) {
579
- this.xref.parse(recoveryMode);
580
- this.catalog = new _obj.Catalog(this.pdfManager, this.xref);
581
- }
582
- }, {
583
- key: "_getLinearizationPage",
584
- value: function _getLinearizationPage(pageIndex) {
585
- var catalog = this.catalog,
586
- linearization = this.linearization;
587
- (0, _util.assert)(linearization && linearization.pageFirst === pageIndex);
588
- var ref = new _primitives.Ref(linearization.objectNumberFirst, 0);
589
- return this.xref.fetchAsync(ref).then(function (obj) {
590
- if ((0, _primitives.isDict)(obj, 'Page') || (0, _primitives.isDict)(obj) && !obj.has('Type') && obj.has('Contents')) {
591
- if (ref && !catalog.pageKidsCountCache.has(ref)) {
592
- catalog.pageKidsCountCache.put(ref, 1);
593
- }
536
+ if (found) {
537
+ stream.skip(9);
538
+ let ch;
539
+
540
+ do {
541
+ ch = stream.getByte();
542
+ } while ((0, _core_utils.isWhiteSpace)(ch));
594
543
 
595
- return [obj, ref];
544
+ let str = "";
545
+
546
+ while (ch >= 0x20 && ch <= 0x39) {
547
+ str += String.fromCharCode(ch);
548
+ ch = stream.getByte();
596
549
  }
597
550
 
598
- throw new _util.FormatError('The Linearization dictionary doesn\'t point ' + 'to a valid Page dictionary.');
599
- }).catch(function (reason) {
600
- (0, _util.info)(reason);
601
- return catalog.getPageDict(pageIndex);
602
- });
603
- }
604
- }, {
605
- key: "getPage",
606
- value: function getPage(pageIndex) {
607
- var _this5 = this;
551
+ startXRef = parseInt(str, 10);
608
552
 
609
- if (this._pagePromises[pageIndex] !== undefined) {
610
- return this._pagePromises[pageIndex];
553
+ if (isNaN(startXRef)) {
554
+ startXRef = 0;
555
+ }
611
556
  }
612
-
613
- var catalog = this.catalog,
614
- linearization = this.linearization;
615
- var promise = linearization && linearization.pageFirst === pageIndex ? this._getLinearizationPage(pageIndex) : catalog.getPageDict(pageIndex);
616
- return this._pagePromises[pageIndex] = promise.then(function (_ref10) {
617
- var _ref11 = _slicedToArray(_ref10, 2),
618
- pageDict = _ref11[0],
619
- ref = _ref11[1];
620
-
621
- return new Page({
622
- pdfManager: _this5.pdfManager,
623
- xref: _this5.xref,
624
- pageIndex: pageIndex,
625
- pageDict: pageDict,
626
- ref: ref,
627
- fontCache: catalog.fontCache,
628
- builtInCMapCache: catalog.builtInCMapCache,
629
- pdfFunctionFactory: _this5.pdfFunctionFactory
630
- });
631
- });
632
557
  }
633
- }, {
634
- key: "checkFirstPage",
635
- value: function checkFirstPage() {
636
- var _this6 = this;
637
558
 
638
- return this.getPage(0).catch(function (reason) {
639
- if (reason instanceof _util.XRefEntryException) {
640
- _this6._pagePromises.length = 0;
559
+ return (0, _util.shadow)(this, "startXRef", startXRef);
560
+ }
641
561
 
642
- _this6.cleanup();
562
+ checkHeader() {
563
+ const stream = this.stream;
564
+ stream.reset();
643
565
 
644
- throw new _util.XRefParseException();
645
- }
646
- });
566
+ if (!find(stream, PDF_HEADER_SIGNATURE)) {
567
+ return;
647
568
  }
648
- }, {
649
- key: "fontFallback",
650
- value: function fontFallback(id, handler) {
651
- return this.catalog.fontFallback(id, handler);
652
- }
653
- }, {
654
- key: "cleanup",
655
- value: function cleanup() {
656
- return this.catalog.cleanup();
657
- }
658
- }, {
659
- key: "linearization",
660
- get: function get() {
661
- var linearization = null;
662
-
663
- try {
664
- linearization = _parser.Linearization.create(this.stream);
665
- } catch (err) {
666
- if (err instanceof _util.MissingDataException) {
667
- throw err;
668
- }
669
569
 
670
- (0, _util.info)(err);
570
+ stream.moveStart();
571
+ const MAX_PDF_VERSION_LENGTH = 12;
572
+ let version = "",
573
+ ch;
574
+
575
+ while ((ch = stream.getByte()) > 0x20) {
576
+ if (version.length >= MAX_PDF_VERSION_LENGTH) {
577
+ break;
671
578
  }
672
579
 
673
- return (0, _util.shadow)(this, 'linearization', linearization);
580
+ version += String.fromCharCode(ch);
674
581
  }
675
- }, {
676
- key: "startXRef",
677
- get: function get() {
678
- var stream = this.stream;
679
- var startXRef = 0;
680
582
 
681
- if (this.linearization) {
682
- stream.reset();
583
+ if (!this.pdfFormatVersion) {
584
+ this.pdfFormatVersion = version.substring(5);
585
+ }
586
+ }
683
587
 
684
- if (find(stream, 'endobj', 1024)) {
685
- startXRef = stream.pos + 6;
686
- }
687
- } else {
688
- var step = 1024;
689
- var startXRefLength = 'startxref'.length;
690
- var found = false,
691
- pos = stream.end;
588
+ parseStartXRef() {
589
+ this.xref.setStartXRef(this.startXRef);
590
+ }
591
+
592
+ setup(recoveryMode) {
593
+ this.xref.parse(recoveryMode);
594
+ this.catalog = new _obj.Catalog(this.pdfManager, this.xref);
595
+ }
692
596
 
693
- while (!found && pos > 0) {
694
- pos -= step - startXRefLength;
597
+ get numPages() {
598
+ const linearization = this.linearization;
599
+ const num = linearization ? linearization.numPages : this.catalog.numPages;
600
+ return (0, _util.shadow)(this, "numPages", num);
601
+ }
695
602
 
696
- if (pos < 0) {
697
- pos = 0;
698
- }
603
+ get documentInfo() {
604
+ const DocumentInfoValidators = {
605
+ Title: _util.isString,
606
+ Author: _util.isString,
607
+ Subject: _util.isString,
608
+ Keywords: _util.isString,
609
+ Creator: _util.isString,
610
+ Producer: _util.isString,
611
+ CreationDate: _util.isString,
612
+ ModDate: _util.isString,
613
+ Trapped: _primitives.isName
614
+ };
615
+ let version = this.pdfFormatVersion;
699
616
 
700
- stream.pos = pos;
701
- found = find(stream, 'startxref', step, true);
702
- }
617
+ if (typeof version !== "string" || !PDF_HEADER_VERSION_REGEXP.test(version)) {
618
+ (0, _util.warn)(`Invalid PDF header version number: ${version}`);
619
+ version = null;
620
+ }
621
+
622
+ const docInfo = {
623
+ PDFFormatVersion: version,
624
+ IsLinearized: !!this.linearization,
625
+ IsAcroFormPresent: !!this.acroForm,
626
+ IsXFAPresent: !!this.xfa,
627
+ IsCollectionPresent: !!this.collection
628
+ };
629
+ let infoDict;
703
630
 
704
- if (found) {
705
- stream.skip(9);
706
- var ch;
631
+ try {
632
+ infoDict = this.xref.trailer.get("Info");
633
+ } catch (err) {
634
+ if (err instanceof _core_utils.MissingDataException) {
635
+ throw err;
636
+ }
707
637
 
708
- do {
709
- ch = stream.getByte();
710
- } while ((0, _util.isSpace)(ch));
638
+ (0, _util.info)("The document information dictionary is invalid.");
639
+ }
711
640
 
712
- var str = '';
641
+ if ((0, _primitives.isDict)(infoDict)) {
642
+ for (const key of infoDict.getKeys()) {
643
+ const value = infoDict.get(key);
713
644
 
714
- while (ch >= 0x20 && ch <= 0x39) {
715
- str += String.fromCharCode(ch);
716
- ch = stream.getByte();
645
+ if (DocumentInfoValidators[key]) {
646
+ if (DocumentInfoValidators[key](value)) {
647
+ docInfo[key] = typeof value !== "string" ? value : (0, _util.stringToPDFString)(value);
648
+ } else {
649
+ (0, _util.info)(`Bad value in document info for "${key}".`);
650
+ }
651
+ } else if (typeof key === "string") {
652
+ let customValue;
653
+
654
+ if ((0, _util.isString)(value)) {
655
+ customValue = (0, _util.stringToPDFString)(value);
656
+ } else if ((0, _primitives.isName)(value) || (0, _util.isNum)(value) || (0, _util.isBool)(value)) {
657
+ customValue = value;
658
+ } else {
659
+ (0, _util.info)(`Unsupported value in document info for (custom) "${key}".`);
660
+ continue;
717
661
  }
718
662
 
719
- startXRef = parseInt(str, 10);
720
-
721
- if (isNaN(startXRef)) {
722
- startXRef = 0;
663
+ if (!docInfo.Custom) {
664
+ docInfo.Custom = Object.create(null);
723
665
  }
666
+
667
+ docInfo.Custom[key] = customValue;
724
668
  }
725
669
  }
670
+ }
726
671
 
727
- return (0, _util.shadow)(this, 'startXRef', startXRef);
672
+ return (0, _util.shadow)(this, "documentInfo", docInfo);
673
+ }
674
+
675
+ get fingerprint() {
676
+ let hash;
677
+ const idArray = this.xref.trailer.get("ID");
678
+
679
+ if (Array.isArray(idArray) && idArray[0] && (0, _util.isString)(idArray[0]) && idArray[0] !== EMPTY_FINGERPRINT) {
680
+ hash = (0, _util.stringToBytes)(idArray[0]);
681
+ } else {
682
+ hash = (0, _crypto.calculateMD5)(this.stream.getByteRange(0, FINGERPRINT_FIRST_BYTES), 0, FINGERPRINT_FIRST_BYTES);
728
683
  }
729
- }, {
730
- key: "numPages",
731
- get: function get() {
732
- var linearization = this.linearization;
733
- var num = linearization ? linearization.numPages : this.catalog.numPages;
734
- return (0, _util.shadow)(this, 'numPages', num);
684
+
685
+ const fingerprintBuf = [];
686
+
687
+ for (let i = 0, ii = hash.length; i < ii; i++) {
688
+ const hex = hash[i].toString(16);
689
+ fingerprintBuf.push(hex.padStart(2, "0"));
735
690
  }
736
- }, {
737
- key: "documentInfo",
738
- get: function get() {
739
- var DocumentInfoValidators = {
740
- Title: _util.isString,
741
- Author: _util.isString,
742
- Subject: _util.isString,
743
- Keywords: _util.isString,
744
- Creator: _util.isString,
745
- Producer: _util.isString,
746
- CreationDate: _util.isString,
747
- ModDate: _util.isString,
748
- Trapped: _primitives.isName
749
- };
750
- var docInfo = {
751
- PDFFormatVersion: this.pdfFormatVersion,
752
- IsLinearized: !!this.linearization,
753
- IsAcroFormPresent: !!this.acroForm,
754
- IsXFAPresent: !!this.xfa
755
- };
756
- var infoDict;
757
-
758
- try {
759
- infoDict = this.xref.trailer.get('Info');
760
- } catch (err) {
761
- if (err instanceof _util.MissingDataException) {
762
- throw err;
763
- }
764
691
 
765
- (0, _util.info)('The document information dictionary is invalid.');
766
- }
692
+ return (0, _util.shadow)(this, "fingerprint", fingerprintBuf.join(""));
693
+ }
767
694
 
768
- if ((0, _primitives.isDict)(infoDict)) {
769
- var _iteratorNormalCompletion4 = true;
770
- var _didIteratorError4 = false;
771
- var _iteratorError4 = undefined;
772
-
773
- try {
774
- for (var _iterator4 = infoDict.getKeys()[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
775
- var key = _step4.value;
776
- var value = infoDict.get(key);
777
-
778
- if (DocumentInfoValidators[key]) {
779
- if (DocumentInfoValidators[key](value)) {
780
- docInfo[key] = typeof value !== 'string' ? value : (0, _util.stringToPDFString)(value);
781
- } else {
782
- (0, _util.info)("Bad value in document info for \"".concat(key, "\"."));
783
- }
784
- } else if (typeof key === 'string') {
785
- var customValue = void 0;
786
-
787
- if ((0, _util.isString)(value)) {
788
- customValue = (0, _util.stringToPDFString)(value);
789
- } else if ((0, _primitives.isName)(value) || (0, _util.isNum)(value) || (0, _util.isBool)(value)) {
790
- customValue = value;
791
- } else {
792
- (0, _util.info)("Unsupported value in document info for (custom) \"".concat(key, "\"."));
793
- continue;
794
- }
795
-
796
- if (!docInfo['Custom']) {
797
- docInfo['Custom'] = Object.create(null);
798
- }
799
-
800
- docInfo['Custom'][key] = customValue;
801
- }
802
- }
803
- } catch (err) {
804
- _didIteratorError4 = true;
805
- _iteratorError4 = err;
806
- } finally {
807
- try {
808
- if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
809
- _iterator4.return();
810
- }
811
- } finally {
812
- if (_didIteratorError4) {
813
- throw _iteratorError4;
814
- }
815
- }
816
- }
817
- }
695
+ _getLinearizationPage(pageIndex) {
696
+ const {
697
+ catalog,
698
+ linearization
699
+ } = this;
818
700
 
819
- return (0, _util.shadow)(this, 'documentInfo', docInfo);
820
- }
821
- }, {
822
- key: "fingerprint",
823
- get: function get() {
824
- var hash;
825
- var idArray = this.xref.trailer.get('ID');
826
-
827
- if (Array.isArray(idArray) && idArray[0] && (0, _util.isString)(idArray[0]) && idArray[0] !== EMPTY_FINGERPRINT) {
828
- hash = (0, _util.stringToBytes)(idArray[0]);
829
- } else {
830
- if (this.stream.ensureRange) {
831
- this.stream.ensureRange(0, Math.min(FINGERPRINT_FIRST_BYTES, this.stream.end));
701
+ const ref = _primitives.Ref.get(linearization.objectNumberFirst, 0);
702
+
703
+ return this.xref.fetchAsync(ref).then(obj => {
704
+ if ((0, _primitives.isDict)(obj, "Page") || (0, _primitives.isDict)(obj) && !obj.has("Type") && obj.has("Contents")) {
705
+ if (ref && !catalog.pageKidsCountCache.has(ref)) {
706
+ catalog.pageKidsCountCache.put(ref, 1);
832
707
  }
833
708
 
834
- hash = (0, _crypto.calculateMD5)(this.stream.bytes.subarray(0, FINGERPRINT_FIRST_BYTES), 0, FINGERPRINT_FIRST_BYTES);
709
+ return [obj, ref];
835
710
  }
836
711
 
837
- var fingerprint = '';
712
+ throw new _util.FormatError("The Linearization dictionary doesn't point " + "to a valid Page dictionary.");
713
+ }).catch(reason => {
714
+ (0, _util.info)(reason);
715
+ return catalog.getPageDict(pageIndex);
716
+ });
717
+ }
838
718
 
839
- for (var i = 0, ii = hash.length; i < ii; i++) {
840
- var hex = hash[i].toString(16);
841
- fingerprint += hex.length === 1 ? '0' + hex : hex;
719
+ getPage(pageIndex) {
720
+ if (this._pagePromises[pageIndex] !== undefined) {
721
+ return this._pagePromises[pageIndex];
722
+ }
723
+
724
+ const {
725
+ catalog,
726
+ linearization
727
+ } = this;
728
+ const promise = linearization && linearization.pageFirst === pageIndex ? this._getLinearizationPage(pageIndex) : catalog.getPageDict(pageIndex);
729
+ return this._pagePromises[pageIndex] = promise.then(([pageDict, ref]) => {
730
+ return new Page({
731
+ pdfManager: this.pdfManager,
732
+ xref: this.xref,
733
+ pageIndex,
734
+ pageDict,
735
+ ref,
736
+ fontCache: catalog.fontCache,
737
+ builtInCMapCache: catalog.builtInCMapCache,
738
+ globalImageCache: catalog.globalImageCache,
739
+ pdfFunctionFactory: this.pdfFunctionFactory
740
+ });
741
+ });
742
+ }
743
+
744
+ checkFirstPage() {
745
+ return this.getPage(0).catch(async reason => {
746
+ if (reason instanceof _core_utils.XRefEntryException) {
747
+ this._pagePromises.length = 0;
748
+ await this.cleanup();
749
+ throw new _core_utils.XRefParseException();
842
750
  }
751
+ });
752
+ }
843
753
 
844
- return (0, _util.shadow)(this, 'fingerprint', fingerprint);
845
- }
846
- }]);
754
+ fontFallback(id, handler) {
755
+ return this.catalog.fontFallback(id, handler);
756
+ }
847
757
 
848
- return PDFDocument;
849
- }();
758
+ async cleanup(manuallyTriggered = false) {
759
+ return this.catalog ? this.catalog.cleanup(manuallyTriggered) : (0, _primitives.clearPrimitiveCaches)();
760
+ }
761
+
762
+ }
850
763
 
851
764
  exports.PDFDocument = PDFDocument;