firefox_print_service.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.FirefoxPrintService = FirefoxPrintService;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _app = require("./app.js");
  29. var _pdf = require("../pdf");
  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. canvas.style.width = Math.floor(size.width * _ui_utils.CSS_UNITS) + "px";
  36. canvas.style.height = Math.floor(size.height * _ui_utils.CSS_UNITS) + "px";
  37. const canvasWrapper = document.createElement("div");
  38. canvasWrapper.appendChild(canvas);
  39. printContainer.appendChild(canvasWrapper);
  40. let currentRenderTask = null;
  41. canvas.mozPrintCallback = function (obj) {
  42. const ctx = obj.context;
  43. ctx.save();
  44. ctx.fillStyle = "rgb(255, 255, 255)";
  45. ctx.fillRect(0, 0, canvas.width, canvas.height);
  46. ctx.restore();
  47. let thisRenderTask = null;
  48. pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  49. if (currentRenderTask) {
  50. currentRenderTask.cancel();
  51. currentRenderTask = null;
  52. }
  53. const renderContext = {
  54. canvasContext: ctx,
  55. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  56. viewport: pdfPage.getViewport({
  57. scale: 1,
  58. rotation: size.rotation
  59. }),
  60. intent: "print",
  61. annotationStorage: pdfDocument.annotationStorage,
  62. optionalContentConfigPromise
  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 (error) {
  72. console.error(error);
  73. if (currentRenderTask === thisRenderTask) {
  74. currentRenderTask.cancel();
  75. currentRenderTask = null;
  76. }
  77. if ("abort" in obj) {
  78. obj.abort();
  79. } else {
  80. obj.done();
  81. }
  82. });
  83. };
  84. }
  85. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise = null) {
  86. this.pdfDocument = pdfDocument;
  87. this.pagesOverview = pagesOverview;
  88. this.printContainer = printContainer;
  89. this._printResolution = printResolution || 150;
  90. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  91. }
  92. FirefoxPrintService.prototype = {
  93. layout() {
  94. const {
  95. pdfDocument,
  96. pagesOverview,
  97. printContainer,
  98. _printResolution,
  99. _optionalContentConfigPromise
  100. } = this;
  101. const body = document.querySelector("body");
  102. body.setAttribute("data-pdfjsprinting", true);
  103. for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
  104. composePage(pdfDocument, i + 1, pagesOverview[i], printContainer, _printResolution, _optionalContentConfigPromise);
  105. }
  106. },
  107. destroy() {
  108. this.printContainer.textContent = "";
  109. const body = document.querySelector("body");
  110. body.removeAttribute("data-pdfjsprinting");
  111. }
  112. };
  113. _app.PDFPrintServiceFactory.instance = {
  114. get supportsPrinting() {
  115. const canvas = document.createElement("canvas");
  116. const value = ("mozPrintCallback" in canvas);
  117. return (0, _pdf.shadow)(this, "supportsPrinting", value);
  118. },
  119. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise) {
  120. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise);
  121. }
  122. };