pdf_document_properties.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. #fieldData = null;
  46. constructor({
  47. dialog,
  48. fields,
  49. closeButton
  50. }, overlayManager, eventBus, l10n, fileNameLookup) {
  51. this.dialog = dialog;
  52. this.fields = fields;
  53. this.overlayManager = overlayManager;
  54. this.l10n = l10n;
  55. this._fileNameLookup = fileNameLookup;
  56. this.#reset();
  57. closeButton.addEventListener("click", this.close.bind(this));
  58. this.overlayManager.register(this.dialog);
  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. await Promise.all([this.overlayManager.open(this.dialog), this._dataAvailableCapability.promise]);
  72. const currentPageNumber = this._currentPageNumber;
  73. const pagesRotation = this._pagesRotation;
  74. if (this.#fieldData && currentPageNumber === this.#fieldData._currentPageNumber && pagesRotation === this.#fieldData._pagesRotation) {
  75. this.#updateUI();
  76. return;
  77. }
  78. const {
  79. info,
  80. contentLength
  81. } = await this.pdfDocument.getMetadata();
  82. const [fileName, fileSize, creationDate, modificationDate, pageSize, isLinearized] = await Promise.all([this._fileNameLookup(), this.#parseFileSize(contentLength), this.#parseDate(info.CreationDate), this.#parseDate(info.ModDate), this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
  83. return this.#parsePageSize((0, _ui_utils.getPageSizeInches)(pdfPage), pagesRotation);
  84. }), this.#parseLinearization(info.IsLinearized)]);
  85. this.#fieldData = Object.freeze({
  86. fileName,
  87. fileSize,
  88. title: info.Title,
  89. author: info.Author,
  90. subject: info.Subject,
  91. keywords: info.Keywords,
  92. creationDate,
  93. modificationDate,
  94. creator: info.Creator,
  95. producer: info.Producer,
  96. version: info.PDFFormatVersion,
  97. pageCount: this.pdfDocument.numPages,
  98. pageSize,
  99. linearized: isLinearized,
  100. _currentPageNumber: currentPageNumber,
  101. _pagesRotation: pagesRotation
  102. });
  103. this.#updateUI();
  104. const {
  105. length
  106. } = await this.pdfDocument.getDownloadInfo();
  107. if (contentLength === length) {
  108. return;
  109. }
  110. const data = Object.assign(Object.create(null), this.#fieldData);
  111. data.fileSize = await this.#parseFileSize(length);
  112. this.#fieldData = Object.freeze(data);
  113. this.#updateUI();
  114. }
  115. async close() {
  116. this.overlayManager.close(this.dialog);
  117. }
  118. setDocument(pdfDocument) {
  119. if (this.pdfDocument) {
  120. this.#reset();
  121. this.#updateUI(true);
  122. }
  123. if (!pdfDocument) {
  124. return;
  125. }
  126. this.pdfDocument = pdfDocument;
  127. this._dataAvailableCapability.resolve();
  128. }
  129. #reset() {
  130. this.pdfDocument = null;
  131. this.#fieldData = null;
  132. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  133. this._currentPageNumber = 1;
  134. this._pagesRotation = 0;
  135. }
  136. #updateUI(reset = false) {
  137. if (reset || !this.#fieldData) {
  138. for (const id in this.fields) {
  139. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  140. }
  141. return;
  142. }
  143. if (this.overlayManager.active !== this.dialog) {
  144. return;
  145. }
  146. for (const id in this.fields) {
  147. const content = this.#fieldData[id];
  148. this.fields[id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  149. }
  150. }
  151. async #parseFileSize(fileSize = 0) {
  152. const kb = fileSize / 1024,
  153. mb = kb / 1024;
  154. if (!kb) {
  155. return undefined;
  156. }
  157. return this.l10n.get(`document_properties_${mb >= 1 ? "mb" : "kb"}`, {
  158. size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
  159. size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
  160. size_b: fileSize.toLocaleString()
  161. });
  162. }
  163. async #parsePageSize(pageSizeInches, pagesRotation) {
  164. if (!pageSizeInches) {
  165. return undefined;
  166. }
  167. if (pagesRotation % 180 !== 0) {
  168. pageSizeInches = {
  169. width: pageSizeInches.height,
  170. height: pageSizeInches.width
  171. };
  172. }
  173. const isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  174. let sizeInches = {
  175. width: Math.round(pageSizeInches.width * 100) / 100,
  176. height: Math.round(pageSizeInches.height * 100) / 100
  177. };
  178. let sizeMillimeters = {
  179. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  180. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  181. };
  182. let rawName = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  183. if (!rawName && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  184. const exactMillimeters = {
  185. width: pageSizeInches.width * 25.4,
  186. height: pageSizeInches.height * 25.4
  187. };
  188. const intMillimeters = {
  189. width: Math.round(sizeMillimeters.width),
  190. height: Math.round(sizeMillimeters.height)
  191. };
  192. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  193. rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  194. if (rawName) {
  195. sizeInches = {
  196. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  197. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  198. };
  199. sizeMillimeters = intMillimeters;
  200. }
  201. }
  202. }
  203. const [{
  204. width,
  205. height
  206. }, 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"}`)]);
  207. return this.l10n.get(`document_properties_page_size_dimension_${name ? "name_" : ""}string`, {
  208. width: width.toLocaleString(),
  209. height: height.toLocaleString(),
  210. unit,
  211. name,
  212. orientation
  213. });
  214. }
  215. async #parseDate(inputDate) {
  216. const dateObject = _pdf.PDFDateString.toDateObject(inputDate);
  217. if (!dateObject) {
  218. return undefined;
  219. }
  220. return this.l10n.get("document_properties_date_string", {
  221. date: dateObject.toLocaleDateString(),
  222. time: dateObject.toLocaleTimeString()
  223. });
  224. }
  225. #parseLinearization(isLinearized) {
  226. return this.l10n.get(`document_properties_linearized_${isLinearized ? "yes" : "no"}`);
  227. }
  228. }
  229. exports.PDFDocumentProperties = PDFDocumentProperties;