pdf_document_properties.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. 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. Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(() => {
  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. this.pdfDocument.getMetadata().then(({
  87. info,
  88. metadata,
  89. contentDispositionFilename
  90. }) => {
  91. return Promise.all([info, metadata, contentDispositionFilename || (0, _ui_utils.getPDFFileNameFromURL)(this.url), this._parseFileSize(this.maybeFileSize), 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. }).then(([info, metadata, fileName, fileSize, creationDate, modDate, pageSize, isLinearized]) => {
  95. freezeFieldData({
  96. fileName,
  97. fileSize,
  98. title: info.Title,
  99. author: info.Author,
  100. subject: info.Subject,
  101. keywords: info.Keywords,
  102. creationDate,
  103. modificationDate: modDate,
  104. creator: info.Creator,
  105. producer: info.Producer,
  106. version: info.PDFFormatVersion,
  107. pageCount: this.pdfDocument.numPages,
  108. pageSize,
  109. linearized: isLinearized,
  110. _currentPageNumber: currentPageNumber,
  111. _pagesRotation: pagesRotation
  112. });
  113. this._updateUI();
  114. return this.pdfDocument.getDownloadInfo();
  115. }).then(({
  116. length
  117. }) => {
  118. this.maybeFileSize = length;
  119. return this._parseFileSize(length);
  120. }).then(fileSize => {
  121. if (fileSize === this.fieldData.fileSize) {
  122. return;
  123. }
  124. const data = Object.assign(Object.create(null), this.fieldData);
  125. data.fileSize = fileSize;
  126. freezeFieldData(data);
  127. this._updateUI();
  128. });
  129. });
  130. }
  131. close() {
  132. this.overlayManager.close(this.overlayName);
  133. }
  134. setDocument(pdfDocument, url = null) {
  135. if (this.pdfDocument) {
  136. this._reset();
  137. this._updateUI(true);
  138. }
  139. if (!pdfDocument) {
  140. return;
  141. }
  142. this.pdfDocument = pdfDocument;
  143. this.url = url;
  144. this._dataAvailableCapability.resolve();
  145. }
  146. setFileSize(fileSize) {
  147. if (Number.isInteger(fileSize) && fileSize > 0) {
  148. this.maybeFileSize = fileSize;
  149. }
  150. }
  151. _reset() {
  152. this.pdfDocument = null;
  153. this.url = null;
  154. this.maybeFileSize = 0;
  155. delete this.fieldData;
  156. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  157. this._currentPageNumber = 1;
  158. this._pagesRotation = 0;
  159. }
  160. _updateUI(reset = false) {
  161. if (reset || !this.fieldData) {
  162. for (const id in this.fields) {
  163. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  164. }
  165. return;
  166. }
  167. if (this.overlayManager.active !== this.overlayName) {
  168. return;
  169. }
  170. for (const id in this.fields) {
  171. const content = this.fieldData[id];
  172. this.fields[id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  173. }
  174. }
  175. async _parseFileSize(fileSize = 0) {
  176. const kb = fileSize / 1024;
  177. if (!kb) {
  178. return undefined;
  179. } else if (kb < 1024) {
  180. return this.l10n.get("document_properties_kb", {
  181. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  182. size_b: fileSize.toLocaleString()
  183. }, "{{size_kb}} KB ({{size_b}} bytes)");
  184. }
  185. return this.l10n.get("document_properties_mb", {
  186. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  187. size_b: fileSize.toLocaleString()
  188. }, "{{size_mb}} MB ({{size_b}} bytes)");
  189. }
  190. async _parsePageSize(pageSizeInches, pagesRotation) {
  191. if (!pageSizeInches) {
  192. return undefined;
  193. }
  194. if (pagesRotation % 180 !== 0) {
  195. pageSizeInches = {
  196. width: pageSizeInches.height,
  197. height: pageSizeInches.width
  198. };
  199. }
  200. const isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
  201. let sizeInches = {
  202. width: Math.round(pageSizeInches.width * 100) / 100,
  203. height: Math.round(pageSizeInches.height * 100) / 100
  204. };
  205. let sizeMillimeters = {
  206. width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
  207. height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
  208. };
  209. let pageName = null;
  210. let rawName = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
  211. if (!rawName && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
  212. const exactMillimeters = {
  213. width: pageSizeInches.width * 25.4,
  214. height: pageSizeInches.height * 25.4
  215. };
  216. const intMillimeters = {
  217. width: Math.round(sizeMillimeters.width),
  218. height: Math.round(sizeMillimeters.height)
  219. };
  220. if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
  221. rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
  222. if (rawName) {
  223. sizeInches = {
  224. width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
  225. height: Math.round(intMillimeters.height / 25.4 * 100) / 100
  226. };
  227. sizeMillimeters = intMillimeters;
  228. }
  229. }
  230. }
  231. if (rawName) {
  232. pageName = this.l10n.get("document_properties_page_size_name_" + rawName.toLowerCase(), null, rawName);
  233. }
  234. 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(([{
  235. width,
  236. height
  237. }, unit, name, orientation]) => {
  238. return this.l10n.get("document_properties_page_size_dimension_" + (name ? "name_" : "") + "string", {
  239. width: width.toLocaleString(),
  240. height: height.toLocaleString(),
  241. unit,
  242. name,
  243. orientation
  244. }, "{{width}} × {{height}} {{unit}} (" + (name ? "{{name}}, " : "") + "{{orientation}})");
  245. });
  246. }
  247. async _parseDate(inputDate) {
  248. const dateObject = _pdf.PDFDateString.toDateObject(inputDate);
  249. if (!dateObject) {
  250. return undefined;
  251. }
  252. return this.l10n.get("document_properties_date_string", {
  253. date: dateObject.toLocaleDateString(),
  254. time: dateObject.toLocaleTimeString()
  255. }, "{{date}}, {{time}}");
  256. }
  257. _parseLinearization(isLinearized) {
  258. return this.l10n.get("document_properties_linearized_" + (isLinearized ? "yes" : "no"), null, isLinearized ? "Yes" : "No");
  259. }
  260. }
  261. exports.PDFDocumentProperties = PDFDocumentProperties;