pdf_document_properties.js 13 KB

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