firefox_print_service.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.FirefoxPrintService = FirefoxPrintService;
  27. var _pdf = require("../pdf");
  28. var _print_utils = require("./print_utils.js");
  29. var _app = require("./app.js");
  30. function composePage(pdfDocument, pageNumber, size, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise) {
  31. const canvas = document.createElement("canvas");
  32. const PRINT_UNITS = printResolution / _pdf.PixelsPerInch.PDF;
  33. canvas.width = Math.floor(size.width * PRINT_UNITS);
  34. canvas.height = Math.floor(size.height * PRINT_UNITS);
  35. const canvasWrapper = document.createElement("div");
  36. canvasWrapper.className = "printedPage";
  37. canvasWrapper.append(canvas);
  38. printContainer.append(canvasWrapper);
  39. let currentRenderTask = null;
  40. canvas.mozPrintCallback = function (obj) {
  41. const ctx = obj.context;
  42. ctx.save();
  43. ctx.fillStyle = "rgb(255, 255, 255)";
  44. ctx.fillRect(0, 0, canvas.width, canvas.height);
  45. ctx.restore();
  46. let thisRenderTask = null;
  47. Promise.all([pdfDocument.getPage(pageNumber), printAnnotationStoragePromise]).then(function ([pdfPage, printAnnotationStorage]) {
  48. if (currentRenderTask) {
  49. currentRenderTask.cancel();
  50. currentRenderTask = null;
  51. }
  52. const renderContext = {
  53. canvasContext: ctx,
  54. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  55. viewport: pdfPage.getViewport({
  56. scale: 1,
  57. rotation: size.rotation
  58. }),
  59. intent: "print",
  60. annotationMode: _pdf.AnnotationMode.ENABLE_STORAGE,
  61. optionalContentConfigPromise,
  62. printAnnotationStorage
  63. };
  64. currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
  65. return thisRenderTask.promise;
  66. }).then(function () {
  67. if (currentRenderTask === thisRenderTask) {
  68. currentRenderTask = null;
  69. }
  70. obj.done();
  71. }, function (reason) {
  72. if (!(reason instanceof _pdf.RenderingCancelledException)) {
  73. console.error(reason);
  74. }
  75. if (currentRenderTask === thisRenderTask) {
  76. currentRenderTask.cancel();
  77. currentRenderTask = null;
  78. }
  79. if ("abort" in obj) {
  80. obj.abort();
  81. } else {
  82. obj.done();
  83. }
  84. });
  85. };
  86. }
  87. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null, printAnnotationStoragePromise = null) {
  88. this.pdfDocument = pdfDocument;
  89. this.pagesOverview = pagesOverview;
  90. this.printContainer = printContainer;
  91. this._printResolution = printResolution || 150;
  92. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  93. this._printAnnotationStoragePromise = printAnnotationStoragePromise || Promise.resolve();
  94. }
  95. FirefoxPrintService.prototype = {
  96. layout() {
  97. const {
  98. pdfDocument,
  99. pagesOverview,
  100. printContainer,
  101. _printResolution,
  102. _optionalContentConfigPromise,
  103. _printAnnotationStoragePromise
  104. } = this;
  105. const body = document.querySelector("body");
  106. body.setAttribute("data-pdfjsprinting", true);
  107. if (pdfDocument.isPureXfa) {
  108. (0, _print_utils.getXfaHtmlForPrinting)(printContainer, pdfDocument);
  109. return;
  110. }
  111. for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
  112. composePage(pdfDocument, i + 1, pagesOverview[i], printContainer, _printResolution, _optionalContentConfigPromise, _printAnnotationStoragePromise);
  113. }
  114. },
  115. destroy() {
  116. this.printContainer.textContent = "";
  117. const body = document.querySelector("body");
  118. body.removeAttribute("data-pdfjsprinting");
  119. }
  120. };
  121. _app.PDFPrintServiceFactory.instance = {
  122. get supportsPrinting() {
  123. const canvas = document.createElement("canvas");
  124. const value = ("mozPrintCallback" in canvas);
  125. return (0, _pdf.shadow)(this, "supportsPrinting", value);
  126. },
  127. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise) {
  128. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, printAnnotationStoragePromise);
  129. }
  130. };