firefox_print_service.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  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(1, size.rotation),
  52. intent: 'print'
  53. };
  54. return pdfPage.render(renderContext).promise;
  55. }).then(function () {
  56. obj.done();
  57. }, function (error) {
  58. console.error(error);
  59. if ('abort' in obj) {
  60. obj.abort();
  61. } else {
  62. obj.done();
  63. }
  64. });
  65. };
  66. }
  67. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
  68. this.pdfDocument = pdfDocument;
  69. this.pagesOverview = pagesOverview;
  70. this.printContainer = printContainer;
  71. }
  72. FirefoxPrintService.prototype = {
  73. layout: function layout() {
  74. var pdfDocument = this.pdfDocument;
  75. var printContainer = this.printContainer;
  76. var body = document.querySelector('body');
  77. body.setAttribute('data-pdfjsprinting', true);
  78. for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
  79. composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
  80. }
  81. },
  82. destroy: function destroy() {
  83. this.printContainer.textContent = '';
  84. }
  85. };
  86. _app.PDFPrintServiceFactory.instance = {
  87. get supportsPrinting() {
  88. var canvas = document.createElement('canvas');
  89. var value = 'mozPrintCallback' in canvas;
  90. return (0, _pdf.shadow)(this, 'supportsPrinting', value);
  91. },
  92. createPrintService: function createPrintService(pdfDocument, pagesOverview, printContainer) {
  93. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
  94. }
  95. };
  96. exports.FirefoxPrintService = FirefoxPrintService;