2
0

firefox_print_service.js 4.3 KB

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