firefox_print_service.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.FirefoxPrintService = undefined;
  20. var _ui_utils = require('./ui_utils');
  21. var _app = require('./app');
  22. var _pdfjs = require('./pdfjs');
  23. function composePage(pdfDocument, pageNumber, size, printContainer) {
  24. var canvas = document.createElement('canvas');
  25. var PRINT_RESOLUTION = 150;
  26. var PRINT_UNITS = PRINT_RESOLUTION / 72.0;
  27. canvas.width = Math.floor(size.width * PRINT_UNITS);
  28. canvas.height = Math.floor(size.height * PRINT_UNITS);
  29. canvas.style.width = Math.floor(size.width * _ui_utils.CSS_UNITS) + 'px';
  30. canvas.style.height = Math.floor(size.height * _ui_utils.CSS_UNITS) + 'px';
  31. var canvasWrapper = document.createElement('div');
  32. canvasWrapper.appendChild(canvas);
  33. printContainer.appendChild(canvasWrapper);
  34. canvas.mozPrintCallback = function (obj) {
  35. var ctx = obj.context;
  36. ctx.save();
  37. ctx.fillStyle = 'rgb(255, 255, 255)';
  38. ctx.fillRect(0, 0, canvas.width, canvas.height);
  39. ctx.restore();
  40. pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  41. var renderContext = {
  42. canvasContext: ctx,
  43. transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  44. viewport: pdfPage.getViewport(1, size.rotation),
  45. intent: 'print'
  46. };
  47. return pdfPage.render(renderContext).promise;
  48. }).then(function () {
  49. obj.done();
  50. }, function (error) {
  51. console.error(error);
  52. if ('abort' in obj) {
  53. obj.abort();
  54. } else {
  55. obj.done();
  56. }
  57. });
  58. };
  59. }
  60. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
  61. this.pdfDocument = pdfDocument;
  62. this.pagesOverview = pagesOverview;
  63. this.printContainer = printContainer;
  64. }
  65. FirefoxPrintService.prototype = {
  66. layout: function layout() {
  67. var pdfDocument = this.pdfDocument;
  68. var printContainer = this.printContainer;
  69. var body = document.querySelector('body');
  70. body.setAttribute('data-pdfjsprinting', true);
  71. for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
  72. composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
  73. }
  74. },
  75. destroy: function destroy() {
  76. this.printContainer.textContent = '';
  77. }
  78. };
  79. _app.PDFPrintServiceFactory.instance = {
  80. get supportsPrinting() {
  81. var canvas = document.createElement('canvas');
  82. var value = 'mozPrintCallback' in canvas;
  83. return (0, _pdfjs.shadow)(this, 'supportsPrinting', value);
  84. },
  85. createPrintService: function createPrintService(pdfDocument, pagesOverview, printContainer) {
  86. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
  87. }
  88. };
  89. exports.FirefoxPrintService = FirefoxPrintService;