pdf_document_properties.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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) {
  51. this.dialog = dialog;
  52. this.fields = fields;
  53. this.overlayManager = overlayManager;
  54. this.l10n = l10n;
  55. this.#reset();
  56. closeButton.addEventListener("click", this.close.bind(this));
  57. this.overlayManager.register(this.dialog);
  58. eventBus._on("pagechanging", evt => {
  59. this._currentPageNumber = evt.pageNumber;
  60. });
  61. eventBus._on("rotationchanging", evt => {
  62. this._pagesRotation = evt.pagesRotation;
  63. });
  64. this._isNonMetricLocale = true;
  65. l10n.getLanguage().then(locale => {
  66. this._isNonMetricLocale = NON_METRIC_LOCALES.includes(locale);
  67. });
  68. }
  69. async open() {
  70. await Promise.all([this.overlayManager.open(this.dialog), this._dataAvailableCapability.promise]);
  71. const currentPageNumber = this._currentPageNumber;
  72. const pagesRotation = this._pagesRotation;
  73. if (this.#fieldData && currentPageNumber === this.#fieldData._currentPageNumber && pagesRotation === this.#fieldData._pagesRotation) {
  74. this.#updateUI();
  75. return;
  76. }
  77. const {
  78. info,
  79. contentDispositionFilename,
  80. contentLength
  81. } = await this.pdfDocument.getMetadata();
  82. 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 => {
  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, url = null) {
  119. if (this.pdfDocument) {
  120. this.#reset();
  121. this.#updateUI(true);
  122. }
  123. if (!pdfDocument) {
  124. return;
  125. }
  126. this.pdfDocument = pdfDocument;
  127. this.url = url;
  128. this._dataAvailableCapability.resolve();
  129. }
  130. #reset() {
  131. this.pdfDocument = null;
  132. this.url = null;
  133. this.#fieldData = null;
  134. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  135. this._currentPageNumber = 1;
  136. this._pagesRotation = 0;
  137. }
  138. #updateUI(reset = false) {
  139. if (reset || !this.#fieldData) {
  140. for (const id in this.fields) {
  141. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  142. }
  143. return;
  144. }
  145. if (this.overlayManager.active !== this.dialog) {
  146. return;
  147. }
  148. for (const id in this.fields) {
  149. const content = this.#fieldData[id];
  150. this.fields[id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  151. }
  152. }
  153. async #parseFileSize(fileSize = 0) {
  154. const kb = fileSize / 1024,
  155. mb = kb / 1024;
  156. if (!kb) {
  157. return undefined;
  158. }
  159. return this.l10n.get(`document_properties_${mb >= 1 ? "mb" : "kb"}`, {
  160. size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
  161. size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
  162. size_b: fileSize.toLocaleString()
  163. });
  164. }
  165. async #parsePageSize(pageSizeInches, pagesRotation) {
  166. if (!pageSizeInches) {
  167. return undefined;
  168. }
  169. if (pagesRotation % 180 !== 0) {
  170. pageSizeInches = {
  171. width: pageSizeInches.height,
  172. height: pageSizeInches.width
  173. };
  174. }
  175. const isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  176. let sizeInches = {
  177. width: Math.round(pageSizeInches.width * 100) / 100,
  178. height: Math.round(pageSizeInches.height * 100) / 100
  179. };
  180. let sizeMillimeters = {
  181. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  182. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  183. };
  184. let rawName = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  185. if (!rawName && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  186. const exactMillimeters = {
  187. width: pageSizeInches.width * 25.4,
  188. height: pageSizeInches.height * 25.4
  189. };
  190. const intMillimeters = {
  191. width: Math.round(sizeMillimeters.width),
  192. height: Math.round(sizeMillimeters.height)
  193. };
  194. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  195. rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  196. if (rawName) {
  197. sizeInches = {
  198. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  199. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  200. };
  201. sizeMillimeters = intMillimeters;
  202. }
  203. }
  204. }
  205. const [{
  206. width,
  207. height
  208. }, 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"}`)]);
  209. return this.l10n.get(`document_properties_page_size_dimension_${name ? "name_" : ""}string`, {
  210. width: width.toLocaleString(),
  211. height: height.toLocaleString(),
  212. unit,
  213. name,
  214. orientation
  215. });
  216. }
  217. async #parseDate(inputDate) {
  218. const dateObject = _pdf.PDFDateString.toDateObject(inputDate);
  219. if (!dateObject) {
  220. return undefined;
  221. }
  222. return this.l10n.get("document_properties_date_string", {
  223. date: dateObject.toLocaleDateString(),
  224. time: dateObject.toLocaleTimeString()
  225. });
  226. }
  227. #parseLinearization(isLinearized) {
  228. return this.l10n.get(`document_properties_linearized_${isLinearized ? "yes" : "no"}`);
  229. }
  230. }
  231. exports.PDFDocumentProperties = PDFDocumentProperties;