pdf_document_properties.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 PDFDocumentProperties = function () {
  34. function PDFDocumentProperties(_ref, overlayManager, eventBus) {
  35. var overlayName = _ref.overlayName,
  36. fields = _ref.fields,
  37. container = _ref.container,
  38. closeButton = _ref.closeButton;
  39. var _this = this;
  40. var l10n = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _ui_utils.NullL10n;
  41. _classCallCheck(this, PDFDocumentProperties);
  42. this.overlayName = overlayName;
  43. this.fields = fields;
  44. this.container = container;
  45. this.overlayManager = overlayManager;
  46. this.l10n = l10n;
  47. this._reset();
  48. if (closeButton) {
  49. closeButton.addEventListener('click', this.close.bind(this));
  50. }
  51. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this));
  52. if (eventBus) {
  53. eventBus.on('pagechanging', function (evt) {
  54. _this._currentPageNumber = evt.pageNumber;
  55. });
  56. }
  57. }
  58. _createClass(PDFDocumentProperties, [{
  59. key: 'open',
  60. value: function open() {
  61. var _this2 = this;
  62. var freezeFieldData = function freezeFieldData(data) {
  63. Object.defineProperty(_this2, 'fieldData', {
  64. value: Object.freeze(data),
  65. writable: false,
  66. enumerable: true,
  67. configurable: true
  68. });
  69. };
  70. Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
  71. var currentPageNumber = _this2._currentPageNumber;
  72. if (_this2.fieldData && currentPageNumber === _this2.fieldData['_currentPageNumber']) {
  73. _this2._updateUI();
  74. return;
  75. }
  76. _this2.pdfDocument.getMetadata().then(function (_ref2) {
  77. var info = _ref2.info,
  78. metadata = _ref2.metadata,
  79. contentDispositionFilename = _ref2.contentDispositionFilename;
  80. 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) {
  81. return _this2._parsePageSize(pdfPage.pageSizeInches);
  82. })]);
  83. }).then(function (_ref3) {
  84. var _ref4 = _slicedToArray(_ref3, 7),
  85. info = _ref4[0],
  86. metadata = _ref4[1],
  87. fileName = _ref4[2],
  88. fileSize = _ref4[3],
  89. creationDate = _ref4[4],
  90. modDate = _ref4[5],
  91. pageSizes = _ref4[6];
  92. freezeFieldData({
  93. 'fileName': fileName,
  94. 'fileSize': fileSize,
  95. 'title': info.Title,
  96. 'author': info.Author,
  97. 'subject': info.Subject,
  98. 'keywords': info.Keywords,
  99. 'creationDate': creationDate,
  100. 'modificationDate': modDate,
  101. 'creator': info.Creator,
  102. 'producer': info.Producer,
  103. 'version': info.PDFFormatVersion,
  104. 'pageCount': _this2.pdfDocument.numPages,
  105. 'pageSizeInch': pageSizes.inch,
  106. 'pageSizeMM': pageSizes.mm,
  107. '_currentPageNumber': currentPageNumber
  108. });
  109. _this2._updateUI();
  110. return _this2.pdfDocument.getDownloadInfo();
  111. }).then(function (_ref5) {
  112. var length = _ref5.length;
  113. _this2.maybeFileSize = length;
  114. return _this2._parseFileSize(length);
  115. }).then(function (fileSize) {
  116. if (fileSize === _this2.fieldData['fileSize']) {
  117. return;
  118. }
  119. var data = (0, _ui_utils.cloneObj)(_this2.fieldData);
  120. data['fileSize'] = fileSize;
  121. freezeFieldData(data);
  122. _this2._updateUI();
  123. });
  124. });
  125. }
  126. }, {
  127. key: 'close',
  128. value: function close() {
  129. this.overlayManager.close(this.overlayName);
  130. }
  131. }, {
  132. key: 'setDocument',
  133. value: function setDocument(pdfDocument, url) {
  134. if (this.pdfDocument) {
  135. this._reset();
  136. this._updateUI(true);
  137. }
  138. if (!pdfDocument) {
  139. return;
  140. }
  141. this.pdfDocument = pdfDocument;
  142. this.url = url;
  143. this._dataAvailableCapability.resolve();
  144. }
  145. }, {
  146. key: 'setFileSize',
  147. value: function setFileSize(fileSize) {
  148. if (Number.isInteger(fileSize) && fileSize > 0) {
  149. this.maybeFileSize = fileSize;
  150. }
  151. }
  152. }, {
  153. key: '_reset',
  154. value: function _reset() {
  155. this.pdfDocument = null;
  156. this.url = null;
  157. this.maybeFileSize = 0;
  158. delete this.fieldData;
  159. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  160. this._currentPageNumber = 1;
  161. }
  162. }, {
  163. key: '_updateUI',
  164. value: function _updateUI() {
  165. var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  166. if (reset || !this.fieldData) {
  167. for (var id in this.fields) {
  168. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  169. }
  170. return;
  171. }
  172. if (this.overlayManager.active !== this.overlayName) {
  173. return;
  174. }
  175. for (var _id in this.fields) {
  176. var content = this.fieldData[_id];
  177. this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  178. }
  179. }
  180. }, {
  181. key: '_parseFileSize',
  182. value: function _parseFileSize() {
  183. var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  184. var kb = fileSize / 1024;
  185. if (!kb) {
  186. return Promise.resolve(undefined);
  187. } else if (kb < 1024) {
  188. return this.l10n.get('document_properties_kb', {
  189. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  190. size_b: fileSize.toLocaleString()
  191. }, '{{size_kb}} KB ({{size_b}} bytes)');
  192. }
  193. return this.l10n.get('document_properties_mb', {
  194. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  195. size_b: fileSize.toLocaleString()
  196. }, '{{size_mb}} MB ({{size_b}} bytes)');
  197. }
  198. }, {
  199. key: '_parsePageSize',
  200. value: function _parsePageSize(pageSizeInches) {
  201. if (!pageSizeInches) {
  202. return Promise.resolve({
  203. inch: undefined,
  204. mm: undefined
  205. });
  206. }
  207. var width = pageSizeInches.width,
  208. height = pageSizeInches.height;
  209. return Promise.all([this.l10n.get('document_properties_page_size_in_2', {
  210. width: (Math.round(width * 100) / 100).toLocaleString(),
  211. height: (Math.round(height * 100) / 100).toLocaleString()
  212. }, '{{width}} × {{height}} in'), this.l10n.get('document_properties_page_size_mm_2', {
  213. width: (Math.round(width * 25.4 * 10) / 10).toLocaleString(),
  214. height: (Math.round(height * 25.4 * 10) / 10).toLocaleString()
  215. }, '{{width}} × {{height}} mm')]).then(function (sizes) {
  216. return {
  217. inch: sizes[0],
  218. mm: sizes[1]
  219. };
  220. });
  221. }
  222. }, {
  223. key: '_parseDate',
  224. value: function _parseDate(inputDate) {
  225. if (!inputDate) {
  226. return;
  227. }
  228. var dateToParse = inputDate;
  229. if (dateToParse.substring(0, 2) === 'D:') {
  230. dateToParse = dateToParse.substring(2);
  231. }
  232. var year = parseInt(dateToParse.substring(0, 4), 10);
  233. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  234. var day = parseInt(dateToParse.substring(6, 8), 10);
  235. var hours = parseInt(dateToParse.substring(8, 10), 10);
  236. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  237. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  238. var utRel = dateToParse.substring(14, 15);
  239. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  240. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  241. if (utRel === '-') {
  242. hours += offsetHours;
  243. minutes += offsetMinutes;
  244. } else if (utRel === '+') {
  245. hours -= offsetHours;
  246. minutes -= offsetMinutes;
  247. }
  248. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  249. var dateString = date.toLocaleDateString();
  250. var timeString = date.toLocaleTimeString();
  251. return this.l10n.get('document_properties_date_string', {
  252. date: dateString,
  253. time: timeString
  254. }, '{{date}}, {{time}}');
  255. }
  256. }]);
  257. return PDFDocumentProperties;
  258. }();
  259. exports.PDFDocumentProperties = PDFDocumentProperties;