2
0

firefox_print_service.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  28. var _ui_utils = require("./ui_utils");
  29. var _app = require("./app");
  30. var _pdf = require("../pdf");
  31. function composePage(pdfDocument, pageNumber, size, printContainer) {
  32. var canvas = document.createElement('canvas');
  33. var PRINT_RESOLUTION = _app_options.AppOptions.get('printResolution') || 150;
  34. var 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. var canvasWrapper = document.createElement('div');
  40. canvasWrapper.appendChild(canvas);
  41. printContainer.appendChild(canvasWrapper);
  42. canvas.mozPrintCallback = function (obj) {
  43. var 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. var 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: function layout() {
  78. var pdfDocument = this.pdfDocument,
  79. pagesOverview = this.pagesOverview,
  80. printContainer = this.printContainer;
  81. var body = document.querySelector('body');
  82. body.setAttribute('data-pdfjsprinting', true);
  83. for (var i = 0, ii = pagesOverview.length; i < ii; ++i) {
  84. composePage(pdfDocument, i + 1, pagesOverview[i], printContainer);
  85. }
  86. },
  87. destroy: function destroy() {
  88. this.printContainer.textContent = '';
  89. var body = document.querySelector('body');
  90. body.removeAttribute('data-pdfjsprinting');
  91. }
  92. };
  93. _app.PDFPrintServiceFactory.instance = {
  94. get supportsPrinting() {
  95. var canvas = document.createElement('canvas');
  96. var value = 'mozPrintCallback' in canvas;
  97. return (0, _pdf.shadow)(this, 'supportsPrinting', value);
  98. },
  99. createPrintService: function createPrintService(pdfDocument, pagesOverview, printContainer) {
  100. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
  101. }
  102. };