2
0

firefox_print_service.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_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. 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 (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) {
  88. this.pdfDocument = pdfDocument;
  89. this.pagesOverview = pagesOverview;
  90. this.printContainer = printContainer;
  91. this._printResolution = printResolution || 150;
  92. this._optionalContentConfigPromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
  93. }
  94. FirefoxPrintService.prototype = {
  95. layout() {
  96. const {
  97. pdfDocument,
  98. pagesOverview,
  99. printContainer,
  100. _printResolution,
  101. _optionalContentConfigPromise
  102. } = this;
  103. const body = document.querySelector("body");
  104. body.setAttribute("data-pdfjsprinting", true);
  105. for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
  106. composePage(pdfDocument, i + 1, pagesOverview[i], printContainer, _printResolution, _optionalContentConfigPromise);
  107. }
  108. },
  109. destroy() {
  110. this.printContainer.textContent = "";
  111. const body = document.querySelector("body");
  112. body.removeAttribute("data-pdfjsprinting");
  113. }
  114. };
  115. _app.PDFPrintServiceFactory.instance = {
  116. get supportsPrinting() {
  117. const canvas = document.createElement("canvas");
  118. const value = ("mozPrintCallback" in canvas);
  119. return (0, _pdf.shadow)(this, "supportsPrinting", value);
  120. },
  121. createPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise) {
  122. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise);
  123. }
  124. };