pdf_document_properties.js 9.5 KB

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