pdfjs-dist 2.0.943 → 2.4.456

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