firefox_print_service.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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) {
  31. const canvas = document.createElement("canvas");
  32. const PRINT_UNITS = printResolution / 72.0;
  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.appendChild(canvas);
  38. printContainer.appendChild(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. pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  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. includeAnnotationStorage: true,
  61. optionalContentConfigPromise
  62. };
  63. currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
  64. return thisRenderTask.promise;
  65. }).then(function () {
  66. if (currentRenderTask === thisRenderTask) {
  67. currentRenderTask = null;
  68. }
  69. obj.done();
  70. }, function (reason) {
  71. if (!(reason instanceof _pdf.RenderingCancelledException)) {
  72. console.error(reason);
  73. }
  74. if (currentRenderTask === thisRenderTask) {
  75. currentRenderTask.cancel();
  76. currentRenderTask = null;
  77. }
  78. if ("abort" in obj) {
  79. obj.abort();
  80. } else {
  81. obj.done();
  82. }
  83. });
  84. };
  85. }
  86. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null) {
  87. this.pdfDocument = pdfDocument;
  88. this.pagesOverview = pagesOverview;
  89. this.printContainer = printContainer;
  90. this._printResolution = printResolution || 150;
  91. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  92. }
  93. FirefoxPrintService.prototype = {
  94. layout() {
  95. const {
  96. pdfDocument,
  97. pagesOverview,
  98. printContainer,
  99. _printResolution,
  100. _optionalContentConfigPromise
  101. } = this;
  102. const body = document.querySelector("body");
  103. body.setAttribute("data-pdfjsprinting", true);
  104. if (pdfDocument.isPureXfa) {
  105. (0, _print_utils.getXfaHtmlForPrinting)(printContainer, pdfDocument);
  106. return;
  107. }
  108. for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
  109. composePage(pdfDocument, i + 1, pagesOverview[i], printContainer, _printResolution, _optionalContentConfigPromise);
  110. }
  111. },
  112. destroy() {
  113. this.printContainer.textContent = "";
  114. const body = document.querySelector("body");
  115. body.removeAttribute("data-pdfjsprinting");
  116. }
  117. };
  118. _app.PDFPrintServiceFactory.instance = {
  119. get supportsPrinting() {
  120. const canvas = document.createElement("canvas");
  121. const value = ("mozPrintCallback" in canvas);
  122. return (0, _pdf.shadow)(this, "supportsPrinting", value);
  123. },
  124. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise) {
  125. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise);
  126. }
  127. };