pdfjs-dist 2.2.228 → 2.6.347

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

Potentially problematic release.


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

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