pdf_document_properties.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. const DEFAULT_FIELD_CONTENT = "-";
  30. const NON_METRIC_LOCALES = ["en-us", "en-lr", "my"];
  31. const US_PAGE_NAMES = {
  32. "8.5x11": "Letter",
  33. "8.5x14": "Legal"
  34. };
  35. const METRIC_PAGE_NAMES = {
  36. "297x420": "A3",
  37. "210x297": "A4"
  38. };
  39. function getPageName(size, isPortrait, pageNames) {
  40. const width = isPortrait ? size.width : size.height;
  41. const height = isPortrait ? size.height : size.width;
  42. return pageNames[`${width}x${height}`];
  43. }
  44. class PDFDocumentProperties {
  45. constructor({
  46. overlayName,
  47. fields,
  48. container,
  49. closeButton
  50. }, overlayManager, eventBus, l10n) {
  51. this.overlayName = overlayName;
  52. this.fields = fields;
  53. this.container = container;
  54. this.overlayManager = overlayManager;
  55. this.l10n = l10n;
  56. this._reset();
  57. closeButton.addEventListener("click", this.close.bind(this));
  58. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this));
  59. eventBus._on("pagechanging", evt => {
  60. this._currentPageNumber = evt.pageNumber;
  61. });
  62. eventBus._on("rotationchanging", evt => {
  63. this._pagesRotation = evt.pagesRotation;
  64. });
  65. this._isNonMetricLocale = true;
  66. l10n.getLanguage().then(locale => {
  67. this._isNonMetricLocale = NON_METRIC_LOCALES.includes(locale);
  68. });
  69. }
  70. async open() {
  71. const freezeFieldData = data => {
  72. Object.defineProperty(this, "fieldData", {
  73. value: Object.freeze(data),
  74. writable: false,
  75. enumerable: true,
  76. configurable: true
  77. });
  78. };
  79. await Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]);
  80. const currentPageNumber = this._currentPageNumber;
  81. const pagesRotation = this._pagesRotation;
  82. if (this.fieldData && currentPageNumber === this.fieldData._currentPageNumber && pagesRotation === this.fieldData._pagesRotation) {
  83. this._updateUI();
  84. return;
  85. }
  86. const {
  87. info,
  88. contentDispositionFilename,
  89. contentLength
  90. } = await this.pdfDocument.getMetadata();
  91. const [fileName, fileSize, creationDate, modificationDate, pageSize, isLinearized] = await Promise.all([contentDispositionFilename || (0, _pdf.getPdfFilenameFromUrl)(this.url), this._parseFileSize(contentLength), this._parseDate(info.CreationDate), this._parseDate(info.ModDate), this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
  92. return this._parsePageSize((0, _ui_utils.getPageSizeInches)(pdfPage), pagesRotation);
  93. }), this._parseLinearization(info.IsLinearized)]);
  94. freezeFieldData({
  95. fileName,
  96. fileSize,
  97. title: info.Title,
  98. author: info.Author,
  99. subject: info.Subject,
  100. keywords: info.Keywords,
  101. creationDate,
  102. modificationDate,
  103. creator: info.Creator,
  104. producer: info.Producer,
  105. version: info.PDFFormatVersion,
  106. pageCount: this.pdfDocument.numPages,
  107. pageSize,
  108. linearized: isLinearized,
  109. _currentPageNumber: currentPageNumber,
  110. _pagesRotation: pagesRotation
  111. });
  112. this._updateUI();
  113. const {
  114. length
  115. } = await this.pdfDocument.getDownloadInfo();
  116. if (contentLength === length) {
  117. return;
  118. }
  119. const data = Object.assign(Object.create(null), this.fieldData);
  120. data.fileSize = await this._parseFileSize(length);
  121. freezeFieldData(data);
  122. this._updateUI();
  123. }
  124. close() {
  125. this.overlayManager.close(this.overlayName);
  126. }
  127. setDocument(pdfDocument, url = null) {
  128. if (this.pdfDocument) {
  129. this._reset();
  130. this._updateUI(true);
  131. }
  132. if (!pdfDocument) {
  133. return;
  134. }
  135. this.pdfDocument = pdfDocument;
  136. this.url = url;
  137. this._dataAvailableCapability.resolve();
  138. }
  139. _reset() {
  140. this.pdfDocument = null;
  141. this.url = null;
  142. delete this.fieldData;
  143. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  144. this._currentPageNumber = 1;
  145. this._pagesRotation = 0;
  146. }
  147. _updateUI(reset = false) {
  148. if (reset || !this.fieldData) {
  149. for (const id in this.fields) {
  150. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  151. }
  152. return;
  153. }
  154. if (this.overlayManager.active !== this.overlayName) {
  155. return;
  156. }
  157. for (const id in this.fields) {
  158. const content = this.fieldData[id];
  159. this.fields[id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  160. }
  161. }
  162. async _parseFileSize(fileSize = 0) {
  163. const kb = fileSize / 1024,
  164. mb = kb / 1024;
  165. if (!kb) {
  166. return undefined;
  167. }
  168. return this.l10n.get(`document_properties_${mb >= 1 ? "mb" : "kb"}`, {
  169. size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
  170. size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
  171. size_b: fileSize.toLocaleString()
  172. });
  173. }
  174. async _parsePageSize(pageSizeInches, pagesRotation) {
  175. if (!pageSizeInches) {
  176. return undefined;
  177. }
  178. if (pagesRotation % 180 !== 0) {
  179. pageSizeInches = {
  180. width: pageSizeInches.height,
  181. height: pageSizeInches.width
  182. };
  183. }
  184. const isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  185. let sizeInches = {
  186. width: Math.round(pageSizeInches.width * 100) / 100,
  187. height: Math.round(pageSizeInches.height * 100) / 100
  188. };
  189. let sizeMillimeters = {
  190. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  191. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  192. };
  193. let rawName = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  194. if (!rawName && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  195. const exactMillimeters = {
  196. width: pageSizeInches.width * 25.4,
  197. height: pageSizeInches.height * 25.4
  198. };
  199. const intMillimeters = {
  200. width: Math.round(sizeMillimeters.width),
  201. height: Math.round(sizeMillimeters.height)
  202. };
  203. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  204. rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  205. if (rawName) {
  206. sizeInches = {
  207. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  208. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  209. };
  210. sizeMillimeters = intMillimeters;
  211. }
  212. }
  213. }
  214. const [{
  215. width,
  216. height
  217. }, unit, name, orientation] = await Promise.all([this._isNonMetricLocale ? sizeInches : sizeMillimeters, this.l10n.get(`document_properties_page_size_unit_${this._isNonMetricLocale ? "inches" : "millimeters"}`), rawName && this.l10n.get(`document_properties_page_size_name_${rawName.toLowerCase()}`), this.l10n.get(`document_properties_page_size_orientation_${isPortrait ? "portrait" : "landscape"}`)]);
  218. return this.l10n.get(`document_properties_page_size_dimension_${name ? "name_" : ""}string`, {
  219. width: width.toLocaleString(),
  220. height: height.toLocaleString(),
  221. unit,
  222. name,
  223. orientation
  224. });
  225. }
  226. async _parseDate(inputDate) {
  227. const dateObject = _pdf.PDFDateString.toDateObject(inputDate);
  228. if (!dateObject) {
  229. return undefined;
  230. }
  231. return this.l10n.get("document_properties_date_string", {
  232. date: dateObject.toLocaleDateString(),
  233. time: dateObject.toLocaleTimeString()
  234. });
  235. }
  236. _parseLinearization(isLinearized) {
  237. return this.l10n.get(`document_properties_linearized_${isLinearized ? "yes" : "no"}`);
  238. }
  239. }
  240. exports.PDFDocumentProperties = PDFDocumentProperties;