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