pdf_document_properties.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFDocumentProperties = void 0;
  27. var _ui_utils = require("./ui_utils");
  28. var _pdf = require("../pdf");
  29. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  30. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  31. 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; }
  32. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  33. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  34. 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); } }
  35. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  36. var DEFAULT_FIELD_CONTENT = '-';
  37. var NON_METRIC_LOCALES = ['en-us', 'en-lr', 'my'];
  38. var US_PAGE_NAMES = {
  39. '8.5x11': 'Letter',
  40. '8.5x14': 'Legal'
  41. };
  42. var METRIC_PAGE_NAMES = {
  43. '297x420': 'A3',
  44. '210x297': 'A4'
  45. };
  46. function getPageName(size, isPortrait, pageNames) {
  47. var width = isPortrait ? size.width : size.height;
  48. var height = isPortrait ? size.height : size.width;
  49. return pageNames["".concat(width, "x").concat(height)];
  50. }
  51. var PDFDocumentProperties =
  52. /*#__PURE__*/
  53. function () {
  54. function PDFDocumentProperties(_ref, overlayManager, eventBus) {
  55. var _this = this;
  56. var overlayName = _ref.overlayName,
  57. fields = _ref.fields,
  58. container = _ref.container,
  59. closeButton = _ref.closeButton;
  60. var l10n = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _ui_utils.NullL10n;
  61. _classCallCheck(this, PDFDocumentProperties);
  62. this.overlayName = overlayName;
  63. this.fields = fields;
  64. this.container = container;
  65. this.overlayManager = overlayManager;
  66. this.l10n = l10n;
  67. this._reset();
  68. if (closeButton) {
  69. closeButton.addEventListener('click', this.close.bind(this));
  70. }
  71. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this));
  72. if (eventBus) {
  73. eventBus.on('pagechanging', function (evt) {
  74. _this._currentPageNumber = evt.pageNumber;
  75. });
  76. eventBus.on('rotationchanging', function (evt) {
  77. _this._pagesRotation = evt.pagesRotation;
  78. });
  79. }
  80. this._isNonMetricLocale = true;
  81. l10n.getLanguage().then(function (locale) {
  82. _this._isNonMetricLocale = NON_METRIC_LOCALES.includes(locale);
  83. });
  84. }
  85. _createClass(PDFDocumentProperties, [{
  86. key: "open",
  87. value: function open() {
  88. var _this2 = this;
  89. var freezeFieldData = function freezeFieldData(data) {
  90. Object.defineProperty(_this2, 'fieldData', {
  91. value: Object.freeze(data),
  92. writable: false,
  93. enumerable: true,
  94. configurable: true
  95. });
  96. };
  97. Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
  98. var currentPageNumber = _this2._currentPageNumber;
  99. var pagesRotation = _this2._pagesRotation;
  100. if (_this2.fieldData && currentPageNumber === _this2.fieldData['_currentPageNumber'] && pagesRotation === _this2.fieldData['_pagesRotation']) {
  101. _this2._updateUI();
  102. return;
  103. }
  104. _this2.pdfDocument.getMetadata().then(function (_ref2) {
  105. var info = _ref2.info,
  106. metadata = _ref2.metadata,
  107. contentDispositionFilename = _ref2.contentDispositionFilename;
  108. return Promise.all([info, metadata, contentDispositionFilename || (0, _ui_utils.getPDFFileNameFromURL)(_this2.url || ''), _this2._parseFileSize(_this2.maybeFileSize), _this2._parseDate(info.CreationDate), _this2._parseDate(info.ModDate), _this2.pdfDocument.getPage(currentPageNumber).then(function (pdfPage) {
  109. return _this2._parsePageSize((0, _ui_utils.getPageSizeInches)(pdfPage), pagesRotation);
  110. }), _this2._parseLinearization(info.IsLinearized)]);
  111. }).then(function (_ref3) {
  112. var _ref4 = _slicedToArray(_ref3, 8),
  113. info = _ref4[0],
  114. metadata = _ref4[1],
  115. fileName = _ref4[2],
  116. fileSize = _ref4[3],
  117. creationDate = _ref4[4],
  118. modDate = _ref4[5],
  119. pageSize = _ref4[6],
  120. isLinearized = _ref4[7];
  121. freezeFieldData({
  122. 'fileName': fileName,
  123. 'fileSize': fileSize,
  124. 'title': info.Title,
  125. 'author': info.Author,
  126. 'subject': info.Subject,
  127. 'keywords': info.Keywords,
  128. 'creationDate': creationDate,
  129. 'modificationDate': modDate,
  130. 'creator': info.Creator,
  131. 'producer': info.Producer,
  132. 'version': info.PDFFormatVersion,
  133. 'pageCount': _this2.pdfDocument.numPages,
  134. 'pageSize': pageSize,
  135. 'linearized': isLinearized,
  136. '_currentPageNumber': currentPageNumber,
  137. '_pagesRotation': pagesRotation
  138. });
  139. _this2._updateUI();
  140. return _this2.pdfDocument.getDownloadInfo();
  141. }).then(function (_ref5) {
  142. var length = _ref5.length;
  143. _this2.maybeFileSize = length;
  144. return _this2._parseFileSize(length);
  145. }).then(function (fileSize) {
  146. if (fileSize === _this2.fieldData['fileSize']) {
  147. return;
  148. }
  149. var data = Object.assign(Object.create(null), _this2.fieldData);
  150. data['fileSize'] = fileSize;
  151. freezeFieldData(data);
  152. _this2._updateUI();
  153. });
  154. });
  155. }
  156. }, {
  157. key: "close",
  158. value: function close() {
  159. this.overlayManager.close(this.overlayName);
  160. }
  161. }, {
  162. key: "setDocument",
  163. value: function setDocument(pdfDocument) {
  164. var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  165. if (this.pdfDocument) {
  166. this._reset();
  167. this._updateUI(true);
  168. }
  169. if (!pdfDocument) {
  170. return;
  171. }
  172. this.pdfDocument = pdfDocument;
  173. this.url = url;
  174. this._dataAvailableCapability.resolve();
  175. }
  176. }, {
  177. key: "setFileSize",
  178. value: function setFileSize(fileSize) {
  179. if (Number.isInteger(fileSize) && fileSize > 0) {
  180. this.maybeFileSize = fileSize;
  181. }
  182. }
  183. }, {
  184. key: "_reset",
  185. value: function _reset() {
  186. this.pdfDocument = null;
  187. this.url = null;
  188. this.maybeFileSize = 0;
  189. delete this.fieldData;
  190. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  191. this._currentPageNumber = 1;
  192. this._pagesRotation = 0;
  193. }
  194. }, {
  195. key: "_updateUI",
  196. value: function _updateUI() {
  197. var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  198. if (reset || !this.fieldData) {
  199. for (var id in this.fields) {
  200. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  201. }
  202. return;
  203. }
  204. if (this.overlayManager.active !== this.overlayName) {
  205. return;
  206. }
  207. for (var _id in this.fields) {
  208. var content = this.fieldData[_id];
  209. this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  210. }
  211. }
  212. }, {
  213. key: "_parseFileSize",
  214. value: function _parseFileSize() {
  215. var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  216. var kb = fileSize / 1024;
  217. if (!kb) {
  218. return Promise.resolve(undefined);
  219. } else if (kb < 1024) {
  220. return this.l10n.get('document_properties_kb', {
  221. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  222. size_b: fileSize.toLocaleString()
  223. }, '{{size_kb}} KB ({{size_b}} bytes)');
  224. }
  225. return this.l10n.get('document_properties_mb', {
  226. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  227. size_b: fileSize.toLocaleString()
  228. }, '{{size_mb}} MB ({{size_b}} bytes)');
  229. }
  230. }, {
  231. key: "_parsePageSize",
  232. value: function _parsePageSize(pageSizeInches, pagesRotation) {
  233. var _this3 = this;
  234. if (!pageSizeInches) {
  235. return Promise.resolve(undefined);
  236. }
  237. if (pagesRotation % 180 !== 0) {
  238. pageSizeInches = {
  239. width: pageSizeInches.height,
  240. height: pageSizeInches.width
  241. };
  242. }
  243. var isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  244. var sizeInches = {
  245. width: Math.round(pageSizeInches.width * 100) / 100,
  246. height: Math.round(pageSizeInches.height * 100) / 100
  247. };
  248. var sizeMillimeters = {
  249. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  250. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  251. };
  252. var pageName = null;
  253. var name = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  254. if (!name && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  255. var exactMillimeters = {
  256. width: pageSizeInches.width * 25.4,
  257. height: pageSizeInches.height * 25.4
  258. };
  259. var intMillimeters = {
  260. width: Math.round(sizeMillimeters.width),
  261. height: Math.round(sizeMillimeters.height)
  262. };
  263. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  264. name = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  265. if (name) {
  266. sizeInches = {
  267. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  268. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  269. };
  270. sizeMillimeters = intMillimeters;
  271. }
  272. }
  273. }
  274. if (name) {
  275. pageName = this.l10n.get('document_properties_page_size_name_' + name.toLowerCase(), null, name);
  276. }
  277. return Promise.all([this._isNonMetricLocale ? sizeInches : sizeMillimeters, this.l10n.get('document_properties_page_size_unit_' + (this._isNonMetricLocale ? 'inches' : 'millimeters'), null, this._isNonMetricLocale ? 'in' : 'mm'), pageName, this.l10n.get('document_properties_page_size_orientation_' + (isPortrait ? 'portrait' : 'landscape'), null, isPortrait ? 'portrait' : 'landscape')]).then(function (_ref6) {
  278. var _ref7 = _slicedToArray(_ref6, 4),
  279. _ref7$ = _ref7[0],
  280. width = _ref7$.width,
  281. height = _ref7$.height,
  282. unit = _ref7[1],
  283. name = _ref7[2],
  284. orientation = _ref7[3];
  285. return _this3.l10n.get('document_properties_page_size_dimension_' + (name ? 'name_' : '') + 'string', {
  286. width: width.toLocaleString(),
  287. height: height.toLocaleString(),
  288. unit: unit,
  289. name: name,
  290. orientation: orientation
  291. }, '{{width}} × {{height}} {{unit}} (' + (name ? '{{name}}, ' : '') + '{{orientation}})');
  292. });
  293. }
  294. }, {
  295. key: "_parseDate",
  296. value: function _parseDate(inputDate) {
  297. if (!inputDate) {
  298. return;
  299. }
  300. var dateToParse = inputDate;
  301. if (dateToParse.substring(0, 2) === 'D:') {
  302. dateToParse = dateToParse.substring(2);
  303. }
  304. var year = parseInt(dateToParse.substring(0, 4), 10);
  305. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  306. var day = parseInt(dateToParse.substring(6, 8), 10);
  307. var hours = parseInt(dateToParse.substring(8, 10), 10);
  308. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  309. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  310. var utRel = dateToParse.substring(14, 15);
  311. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  312. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  313. if (utRel === '-') {
  314. hours += offsetHours;
  315. minutes += offsetMinutes;
  316. } else if (utRel === '+') {
  317. hours -= offsetHours;
  318. minutes -= offsetMinutes;
  319. }
  320. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  321. var dateString = date.toLocaleDateString();
  322. var timeString = date.toLocaleTimeString();
  323. return this.l10n.get('document_properties_date_string', {
  324. date: dateString,
  325. time: timeString
  326. }, '{{date}}, {{time}}');
  327. }
  328. }, {
  329. key: "_parseLinearization",
  330. value: function _parseLinearization(isLinearized) {
  331. return this.l10n.get('document_properties_linearized_' + (isLinearized ? 'yes' : 'no'), null, isLinearized ? 'Yes' : 'No');
  332. }
  333. }]);
  334. return PDFDocumentProperties;
  335. }();
  336. exports.PDFDocumentProperties = PDFDocumentProperties;