2
0

pdf_document_properties.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 = _ui_utils.NullL10n) {
  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, _ui_utils.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. if (!kb) {
  165. return undefined;
  166. } else if (kb < 1024) {
  167. return this.l10n.get("document_properties_kb", {
  168. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  169. size_b: fileSize.toLocaleString()
  170. }, "{{size_kb}} KB ({{size_b}} bytes)");
  171. }
  172. return this.l10n.get("document_properties_mb", {
  173. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  174. size_b: fileSize.toLocaleString()
  175. }, "{{size_mb}} MB ({{size_b}} bytes)");
  176. }
  177. async _parsePageSize(pageSizeInches, pagesRotation) {
  178. if (!pageSizeInches) {
  179. return undefined;
  180. }
  181. if (pagesRotation % 180 !== 0) {
  182. pageSizeInches = {
  183. width: pageSizeInches.height,
  184. height: pageSizeInches.width
  185. };
  186. }
  187. const isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  188. let sizeInches = {
  189. width: Math.round(pageSizeInches.width * 100) / 100,
  190. height: Math.round(pageSizeInches.height * 100) / 100
  191. };
  192. let sizeMillimeters = {
  193. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  194. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  195. };
  196. let pageName = null;
  197. let rawName = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  198. if (!rawName && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  199. const exactMillimeters = {
  200. width: pageSizeInches.width * 25.4,
  201. height: pageSizeInches.height * 25.4
  202. };
  203. const intMillimeters = {
  204. width: Math.round(sizeMillimeters.width),
  205. height: Math.round(sizeMillimeters.height)
  206. };
  207. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  208. rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  209. if (rawName) {
  210. sizeInches = {
  211. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  212. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  213. };
  214. sizeMillimeters = intMillimeters;
  215. }
  216. }
  217. }
  218. if (rawName) {
  219. pageName = this.l10n.get("document_properties_page_size_name_" + rawName.toLowerCase(), null, rawName);
  220. }
  221. return Promise.all([this._isNonMetricLocale ? sizeInches : sizeMillimeters, this.l10n.get("document_properties_page_size_unit_" + (this._isNonMetricLocale ? "inches" : "millimeters"), null, this._isNonMetricLocale ? "in" : "mm"), pageName, this.l10n.get("document_properties_page_size_orientation_" + (isPortrait ? "portrait" : "landscape"), null, isPortrait ? "portrait" : "landscape")]).then(([{
  222. width,
  223. height
  224. }, unit, name, orientation]) => {
  225. return this.l10n.get("document_properties_page_size_dimension_" + (name ? "name_" : "") + "string", {
  226. width: width.toLocaleString(),
  227. height: height.toLocaleString(),
  228. unit,
  229. name,
  230. orientation
  231. }, "{{width}} × {{height}} {{unit}} (" + (name ? "{{name}}, " : "") + "{{orientation}})");
  232. });
  233. }
  234. async _parseDate(inputDate) {
  235. const dateObject = _pdf.PDFDateString.toDateObject(inputDate);
  236. if (!dateObject) {
  237. return undefined;
  238. }
  239. return this.l10n.get("document_properties_date_string", {
  240. date: dateObject.toLocaleDateString(),
  241. time: dateObject.toLocaleTimeString()
  242. }, "{{date}}, {{time}}");
  243. }
  244. _parseLinearization(isLinearized) {
  245. return this.l10n.get("document_properties_linearized_" + (isLinearized ? "yes" : "no"), null, isLinearized ? "Yes" : "No");
  246. }
  247. }
  248. exports.PDFDocumentProperties = PDFDocumentProperties;