pdf_document_properties.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFDocumentProperties = undefined;
  20. var _ui_utils = require('pdfjs-web/ui_utils');
  21. var _overlay_manager = require('pdfjs-web/overlay_manager');
  22. var PDFDocumentProperties = function PDFDocumentPropertiesClosure() {
  23. function PDFDocumentProperties(options) {
  24. this.fields = options.fields;
  25. this.overlayName = options.overlayName;
  26. this.container = options.container;
  27. this.rawFileSize = 0;
  28. this.url = null;
  29. this.pdfDocument = null;
  30. if (options.closeButton) {
  31. options.closeButton.addEventListener('click', this.close.bind(this));
  32. }
  33. this.dataAvailablePromise = new Promise(function (resolve) {
  34. this.resolveDataAvailable = resolve;
  35. }.bind(this));
  36. _overlay_manager.OverlayManager.register(this.overlayName, this.container, this.close.bind(this));
  37. }
  38. PDFDocumentProperties.prototype = {
  39. open: function PDFDocumentProperties_open() {
  40. Promise.all([_overlay_manager.OverlayManager.open(this.overlayName), this.dataAvailablePromise]).then(function () {
  41. this._getProperties();
  42. }.bind(this));
  43. },
  44. close: function PDFDocumentProperties_close() {
  45. _overlay_manager.OverlayManager.close(this.overlayName);
  46. },
  47. setFileSize: function PDFDocumentProperties_setFileSize(fileSize) {
  48. if (fileSize > 0) {
  49. this.rawFileSize = fileSize;
  50. }
  51. },
  52. setDocumentAndUrl: function PDFDocumentProperties_setDocumentAndUrl(pdfDocument, url) {
  53. this.pdfDocument = pdfDocument;
  54. this.url = url;
  55. this.resolveDataAvailable();
  56. },
  57. _getProperties: function PDFDocumentProperties_getProperties() {
  58. if (!_overlay_manager.OverlayManager.active) {
  59. return;
  60. }
  61. this.pdfDocument.getDownloadInfo().then(function (data) {
  62. if (data.length === this.rawFileSize) {
  63. return;
  64. }
  65. this.setFileSize(data.length);
  66. this._updateUI(this.fields['fileSize'], this._parseFileSize());
  67. }.bind(this));
  68. this.pdfDocument.getMetadata().then(function (data) {
  69. var content = {
  70. 'fileName': (0, _ui_utils.getPDFFileNameFromURL)(this.url),
  71. 'fileSize': this._parseFileSize(),
  72. 'title': data.info.Title,
  73. 'author': data.info.Author,
  74. 'subject': data.info.Subject,
  75. 'keywords': data.info.Keywords,
  76. 'creationDate': this._parseDate(data.info.CreationDate),
  77. 'modificationDate': this._parseDate(data.info.ModDate),
  78. 'creator': data.info.Creator,
  79. 'producer': data.info.Producer,
  80. 'version': data.info.PDFFormatVersion,
  81. 'pageCount': this.pdfDocument.numPages
  82. };
  83. for (var identifier in content) {
  84. this._updateUI(this.fields[identifier], content[identifier]);
  85. }
  86. }.bind(this));
  87. },
  88. _updateUI: function PDFDocumentProperties_updateUI(field, content) {
  89. if (field && content !== undefined && content !== '') {
  90. field.textContent = content;
  91. }
  92. },
  93. _parseFileSize: function PDFDocumentProperties_parseFileSize() {
  94. var fileSize = this.rawFileSize,
  95. kb = fileSize / 1024;
  96. if (!kb) {
  97. return;
  98. } else if (kb < 1024) {
  99. return _ui_utils.mozL10n.get('document_properties_kb', {
  100. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  101. size_b: fileSize.toLocaleString()
  102. }, '{{size_kb}} KB ({{size_b}} bytes)');
  103. }
  104. return _ui_utils.mozL10n.get('document_properties_mb', {
  105. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  106. size_b: fileSize.toLocaleString()
  107. }, '{{size_mb}} MB ({{size_b}} bytes)');
  108. },
  109. _parseDate: function PDFDocumentProperties_parseDate(inputDate) {
  110. var dateToParse = inputDate;
  111. if (dateToParse === undefined) {
  112. return '';
  113. }
  114. if (dateToParse.substring(0, 2) === 'D:') {
  115. dateToParse = dateToParse.substring(2);
  116. }
  117. var year = parseInt(dateToParse.substring(0, 4), 10);
  118. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  119. var day = parseInt(dateToParse.substring(6, 8), 10);
  120. var hours = parseInt(dateToParse.substring(8, 10), 10);
  121. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  122. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  123. var utRel = dateToParse.substring(14, 15);
  124. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  125. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  126. if (utRel === '-') {
  127. hours += offsetHours;
  128. minutes += offsetMinutes;
  129. } else if (utRel === '+') {
  130. hours -= offsetHours;
  131. minutes -= offsetMinutes;
  132. }
  133. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  134. var dateString = date.toLocaleDateString();
  135. var timeString = date.toLocaleTimeString();
  136. return _ui_utils.mozL10n.get('document_properties_date_string', {
  137. date: dateString,
  138. time: timeString
  139. }, '{{date}}, {{time}}');
  140. }
  141. };
  142. return PDFDocumentProperties;
  143. }();
  144. exports.PDFDocumentProperties = PDFDocumentProperties;