2
0

firefox_print_service.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
  42. viewport: pdfPage.getViewport(1, size.rotation),
  43. intent: 'print'
  44. };
  45. return pdfPage.render(renderContext).promise;
  46. }).then(function () {
  47. obj.done();
  48. }, function (error) {
  49. console.error(error);
  50. if ('abort' in obj) {
  51. obj.abort();
  52. } else {
  53. obj.done();
  54. }
  55. });
  56. };
  57. }
  58. function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
  59. this.pdfDocument = pdfDocument;
  60. this.pagesOverview = pagesOverview;
  61. this.printContainer = printContainer;
  62. }
  63. FirefoxPrintService.prototype = {
  64. layout: function () {
  65. var pdfDocument = this.pdfDocument;
  66. var printContainer = this.printContainer;
  67. var body = document.querySelector('body');
  68. body.setAttribute('data-pdfjsprinting', true);
  69. for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
  70. composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
  71. }
  72. },
  73. destroy: function () {
  74. this.printContainer.textContent = '';
  75. }
  76. };
  77. PDFPrintServiceFactory.instance = {
  78. get supportsPrinting() {
  79. var canvas = document.createElement('canvas');
  80. var value = 'mozPrintCallback' in canvas;
  81. return pdfjsLib.shadow(this, 'supportsPrinting', value);
  82. },
  83. createPrintService: function (pdfDocument, pagesOverview, printContainer) {
  84. return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
  85. }
  86. };
  87. exports.FirefoxPrintService = FirefoxPrintService;