firefox_print_service.js 3.6 KB

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