crPdf.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CRPDF = void 0;
  6. var _utils = require("../../utils");
  7. var _crProtocolHelper = require("./crProtocolHelper");
  8. /**
  9. * Copyright 2017 Google Inc. All rights reserved.
  10. * Modifications copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. const PagePaperFormats = {
  25. letter: {
  26. width: 8.5,
  27. height: 11
  28. },
  29. legal: {
  30. width: 8.5,
  31. height: 14
  32. },
  33. tabloid: {
  34. width: 11,
  35. height: 17
  36. },
  37. ledger: {
  38. width: 17,
  39. height: 11
  40. },
  41. a0: {
  42. width: 33.1,
  43. height: 46.8
  44. },
  45. a1: {
  46. width: 23.4,
  47. height: 33.1
  48. },
  49. a2: {
  50. width: 16.54,
  51. height: 23.4
  52. },
  53. a3: {
  54. width: 11.7,
  55. height: 16.54
  56. },
  57. a4: {
  58. width: 8.27,
  59. height: 11.7
  60. },
  61. a5: {
  62. width: 5.83,
  63. height: 8.27
  64. },
  65. a6: {
  66. width: 4.13,
  67. height: 5.83
  68. }
  69. };
  70. const unitToPixels = {
  71. 'px': 1,
  72. 'in': 96,
  73. 'cm': 37.8,
  74. 'mm': 3.78
  75. };
  76. function convertPrintParameterToInches(text) {
  77. if (text === undefined) return undefined;
  78. let unit = text.substring(text.length - 2).toLowerCase();
  79. let valueText = '';
  80. if (unitToPixels.hasOwnProperty(unit)) {
  81. valueText = text.substring(0, text.length - 2);
  82. } else {
  83. // In case of unknown unit try to parse the whole parameter as number of pixels.
  84. // This is consistent with phantom's paperSize behavior.
  85. unit = 'px';
  86. valueText = text;
  87. }
  88. const value = Number(valueText);
  89. (0, _utils.assert)(!isNaN(value), 'Failed to parse parameter value: ' + text);
  90. const pixels = value * unitToPixels[unit];
  91. return pixels / 96;
  92. }
  93. class CRPDF {
  94. constructor(client) {
  95. this._client = void 0;
  96. this._client = client;
  97. }
  98. async generate(options) {
  99. const {
  100. scale = 1,
  101. displayHeaderFooter = false,
  102. headerTemplate = '',
  103. footerTemplate = '',
  104. printBackground = false,
  105. landscape = false,
  106. pageRanges = '',
  107. preferCSSPageSize = false,
  108. margin = {}
  109. } = options;
  110. let paperWidth = 8.5;
  111. let paperHeight = 11;
  112. if (options.format) {
  113. const format = PagePaperFormats[options.format.toLowerCase()];
  114. (0, _utils.assert)(format, 'Unknown paper format: ' + options.format);
  115. paperWidth = format.width;
  116. paperHeight = format.height;
  117. } else {
  118. paperWidth = convertPrintParameterToInches(options.width) || paperWidth;
  119. paperHeight = convertPrintParameterToInches(options.height) || paperHeight;
  120. }
  121. const marginTop = convertPrintParameterToInches(margin.top) || 0;
  122. const marginLeft = convertPrintParameterToInches(margin.left) || 0;
  123. const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
  124. const marginRight = convertPrintParameterToInches(margin.right) || 0;
  125. const result = await this._client.send('Page.printToPDF', {
  126. transferMode: 'ReturnAsStream',
  127. landscape,
  128. displayHeaderFooter,
  129. headerTemplate,
  130. footerTemplate,
  131. printBackground,
  132. scale,
  133. paperWidth,
  134. paperHeight,
  135. marginTop,
  136. marginBottom,
  137. marginLeft,
  138. marginRight,
  139. pageRanges,
  140. preferCSSPageSize
  141. });
  142. return await (0, _crProtocolHelper.readProtocolStream)(this._client, result.stream);
  143. }
  144. }
  145. exports.CRPDF = CRPDF;