firefox_print_service.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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");
  28. var _app = require("./app");
  29. var _pdf = require("../pdf");
  30. function composePage(pdfDocument, pageNumber, size, printContainer) {
  31. var canvas = document.createElement('canvas');
  32. var PRINT_RESOLUTION = 150;
  33. var PRINT_UNITS = PRINT_RESOLUTION / 72.0;
  34. canvas.width = Math.floor(size.width * PRINT_UNITS);
  35. canvas.height = Math.floor(size.height * PRINT_UNITS);
  36. canvas.style.width = Math.floor(size.width * _ui_utils.CSS_UNITS) + 'px';
  37. canvas.style.height = Math.floor(size.height * _ui_utils.CSS_UNITS) + 'px';
  38. var canvasWrapper = document.createElement('div');
  39. canvasWrapper.appendChild(canvas);
  40. printContainer.appendChild(canvasWrapper);
  41. canvas.mozPrintCallback = function (obj) {
  42. var 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. pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  48. var renderContext = {
  49. canvasContext: ctx,
  50. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  51. viewport: pdfPage.getViewport({
  52. scale: 1,
  53. rotation: size.rotation
  54. }),
  55. intent: 'print'
  56. };
  57. return pdfPage.render(renderContext).promise;
  58. }).then(function () {
  59. obj.done();
  60. }, function (error) {
  61. console.error(error);
  62. if ('abort' in obj) {
  63. obj.abort();
  64. } else {
  65. obj.done();
  66. }
  67. });
  68. };
  69. }
  70. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
  71. this.pdfDocument = pdfDocument;
  72. this.pagesOverview = pagesOverview;
  73. this.printContainer = printContainer;
  74. }
  75. FirefoxPrintService.prototype = {
  76. layout: function layout() {
  77. var pdfDocument = this.pdfDocument;
  78. var printContainer = this.printContainer;
  79. var body = document.querySelector('body');
  80. body.setAttribute('data-pdfjsprinting', true);
  81. for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
  82. composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
  83. }
  84. },
  85. destroy: function destroy() {
  86. this.printContainer.textContent = '';
  87. }
  88. };
  89. _app.PDFPrintServiceFactory.instance = {
  90. get supportsPrinting() {
  91. var canvas = document.createElement('canvas');
  92. var value = 'mozPrintCallback' in canvas;
  93. return (0, _pdf.shadow)(this, 'supportsPrinting', value);
  94. },
  95. createPrintService: function createPrintService(pdfDocument, pagesOverview, printContainer) {
  96. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
  97. }
  98. };