pdf_document_properties.js 10 KB

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