How to use convertPrintParameterToInches method in Puppeteer

Best JavaScript code snippet using puppeteer

Page.js

Source:Page.js Github

copy

Full Screen

...514 console.assert(format, 'Unknown paper format: ' + options.format);515 paperWidth = format.width;516 paperHeight = format.height;517 } else {518 paperWidth = convertPrintParameterToInches(options.width) || paperWidth;519 paperHeight = convertPrintParameterToInches(options.height) || paperHeight;520 }521 const marginOptions = options.margin || {};522 const marginTop = convertPrintParameterToInches(marginOptions.top) || 0;523 const marginLeft = convertPrintParameterToInches(marginOptions.left) || 0;524 const marginBottom = convertPrintParameterToInches(marginOptions.bottom) || 0;525 const marginRight = convertPrintParameterToInches(marginOptions.right) || 0;526 const result = await this._client.send('Page.printToPDF', {527 landscape: landscape,528 displayHeaderFooter: displayHeaderFooter,529 printBackground: printBackground,530 scale: scale,531 paperWidth: paperWidth,532 paperHeight: paperHeight,533 marginTop: marginTop,534 marginBottom: marginBottom,535 marginLeft: marginLeft,536 marginRight: marginRight,537 pageRanges: pageRanges538 });539 const buffer = new Buffer(result.data, 'base64');540 if (options.path)541 fs.writeFileSync(options.path, buffer);542 return buffer;543 }544 /**545 * @return {!Promise<string>}546 */547 async plainText() {548 return this.evaluate(() => document.body.innerText);549 }550 /**551 * @return {!Promise<string>}552 */553 async title() {554 return this.mainFrame().title();555 }556 /**557 * @return {!Promise}558 */559 async close() {560 await this._client.dispose();561 }562 /**563 * @return {!Mouse}564 */565 get mouse() {566 return this._mouse;567 }568 /**569 * @param {string} selector570 * @param {!Object} options571 * @return {!Promise}572 */573 async click(selector, options) {574 const handle = await this.$(selector);575 console.assert(handle, 'No node found for selector: ' + selector);576 await handle.click(options);577 await handle.dispose();578 }579 /**580 * @param {string} selector581 * @param {!Promise}582 */583 async hover(selector) {584 const handle = await this.$(selector);585 console.assert(handle, 'No node found for selector: ' + selector);586 await handle.hover();587 await handle.dispose();588 }589 /**590 * @param {string} selector591 * @return {!Promise}592 */593 async focus(selector) {594 const handle = await this.$(selector);595 console.assert(handle, 'No node found for selector: ' + selector);596 await handle.evaluate(element => element.focus());597 await handle.dispose();598 }599 /**600 * @param {string} text601 * @param {{delay: (number|undefined)}=} options602 * @return {!Promise}603 */604 async type(text, options) {605 let delay = 0;606 if (options && options.delay)607 delay = options.delay;608 let last;609 for (const char of text) {610 last = this.press(char, {text: char, delay});611 if (delay)612 await new Promise(f => setTimeout(f, delay));613 }614 await last;615 }616 /**617 * @param {string} text618 * @param {!Object=} options619 * @return {!Promise}620 */621 async press(key, options) {622 this._keyboard.down(key, options);623 if (options && options.delay)624 await new Promise(f => setTimeout(f, options.delay));625 await this._keyboard.up(key);626 }627 /**628 * @param {(string|number|function())} selectorOrTimeout629 * @param {!Object=} options630 * @return {!Promise}631 */632 waitFor(selectorOrFunctionOrTimeout, options = {}) {633 return this.mainFrame().waitFor(selectorOrFunctionOrTimeout, options);634 }635 /**636 * @param {string} selector637 * @param {!Object=} options638 * @return {!Promise}639 */640 waitForSelector(selector, options = {}) {641 return this.mainFrame().waitForSelector(selector, options);642 }643 /**644 * @param {function()} pageFunction645 * @param {!Object=} options646 * @param {!Array<*>} args647 * @return {!Promise}648 */649 waitForFunction(pageFunction, options = {}, ...args) {650 return this.mainFrame().waitForFunction(pageFunction, options, ...args);651 }652}653/** @enum {string} */654Page.PaperFormats = {655 letter: {width: 8.5, height: 11},656 legal: {width: 8.5, height: 14},657 tabloid: {width: 11, height: 17},658 ledger: {width: 17, height: 11},659 a0: {width: 33.1, height: 46.8 },660 a1: {width: 23.4, height: 33.1 },661 a2: {width: 16.5, height: 23.4 },662 a3: {width: 11.7, height: 16.5 },663 a4: {width: 8.27, height: 11.7 },664 a5: {width: 5.83, height: 8.27 },665};666const unitToPixels = {667 'px': 1,668 'in': 96,669 'cm': 37.8,670 'mm': 3.78671};672/**673 * @param {(string|number|undefined)} parameter674 * @return {(number|undefined)}675 */676function convertPrintParameterToInches(parameter) {677 if (typeof parameter === 'undefined')678 return undefined;679 let pixels;680 if (helper.isNumber(parameter)) {681 // Treat numbers as pixel values to be aligned with phantom's paperSize.682 pixels = /** @type {number} */ (parameter);683 } else if (helper.isString(parameter)) {684 const text = parameter;685 let unit = text.substring(text.length - 2).toLowerCase();686 let valueText = '';687 if (unitToPixels.hasOwnProperty(unit)) {688 valueText = text.substring(0, text.length - 2);689 } else {690 // In case of unknown unit try to parse the whole parameter as number of pixels....

Full Screen

Full Screen

crPdf.js

Source:crPdf.js Github

copy

Full Screen

...72 'in': 96,73 'cm': 37.8,74 'mm': 3.7875};76function 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}93class 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,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const { convertPrintParameterToInches } = require('puppeteer/lib/PDFUtils');3(async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 await page.pdf({7 margin: convertPrintParameterToInches({8 }),9 });10 await browser.close();11})();12const puppeteer = require('puppeteer');13const { convertPrintParameterToInches } = require('puppeteer/lib/PDFUtils');14(async () => {15 const browser = await puppeteer.launch();16 const page = await browser.newPage();17 await page.pdf({18 margin: convertPrintParameterToInches({19 }),20 });21 await browser.close();22})();23const puppeteer = require('puppeteer');24const { convertPrintParameterToInches } = require('puppeteer/lib/PDFUtils');25(async () => {26 const browser = await puppeteer.launch();27 const page = await browser.newPage();28 await page.pdf({29 margin: convertPrintParameterToInches({30 }),31 });32 await browser.close();33})();34const puppeteer = require('puppeteer');35const { convertPrintParameterToInches } = require('puppeteer/lib/PDFUtils');

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const convertPrintParameterToInches = require('puppeteer/lib/USLetterSize');3const browser = await puppeteer.launch();4const page = await browser.newPage();5const pdf = await page.pdf({6 margin: {7 },8});9await browser.close();10const puppeteer = require('puppeteer');11const convertPrintParameterToInches = require('puppeteer/lib/USLetterSize');12const browser = await puppeteer.launch();13const page = await browser.newPage();14const pdf = await page.pdf({15 margin: {16 },17});18await browser.close();19const puppeteer = require('puppeteer');20const convertPrintParameterToInches = require('puppeteer/lib/USLetterSize');21const browser = await puppeteer.launch();22const page = await browser.newPage();23const pdf = await page.pdf({24 margin: {25 },26});27await browser.close();28const puppeteer = require('puppeteer');29const convertPrintParameterToInches = require('puppeteer/lib/USLetterSize');30const browser = await puppeteer.launch();31const page = await browser.newPage();32const pdf = await page.pdf({33 margin: {

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const { Puppeteer } = require('puppeteer-print');3(async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 const puppeteerPrint = new Puppeteer(page);7 const printParameter = puppeteerPrint.convertPrintParameterToInches({8 margin: {9 },10 });11 await page.pdf(printParameter);12 await browser.close();13})();14import puppeteer from 'puppeteer';15import { Puppeteer } from 'puppeteer-print';16(async () => {17 const browser = await puppeteer.launch();18 const page = await browser.newPage();19 const puppeteerPrint = new Puppeteer(page);20 const printParameter = puppeteerPrint.convertPrintParameterToInches({21 margin: {22 },23 });24 await page.pdf(printParameter);25 await browser.close();26})();27const puppeteer = require('puppeteer');28const { Puppeteer } = require('puppeteer-print');29(async () => {30 const browser = await puppeteer.launch();31 const page = await browser.newPage();32 const puppeteerPrint = new Puppeteer(page);33 const printParameter = puppeteerPrint.convertPrintParameterToMillimeters({34 margin: {35 },36 });37 await page.pdf(printParameter);38 await browser.close();39})();

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const fs = require('fs');3const pdf = async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 await page.setContent(fs.readFileSync('test.html', 'utf8'));7 await page.pdf({8 margin: {9 }10 });11 await browser.close();12};13pdf();14const pdf = async () => {15 const browser = await puppeteer.launch();16 const page = await browser.newPage();17 await page.pdf({18 margin: {19 }20 });21 await browser.close();22};23pdf();24await page.pdf({25 margin: {26 },27 headerTemplate: '<p style="text-align: center; font-size:

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Puppeteer } = require('puppeteer');2const puppeteer = new Puppeteer();3const printParameter = {4};5const convertedPrintParameter = puppeteer.convertPrintParameterToInches(printParameter);6console.log(convertedPrintParameter);7{8}9const { Puppeteer } = require('puppeteer');10const puppeteer = new Puppeteer();11const printParameter = {

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteerPDF = require('puppeteer-pdf');2const puppeteerPDFInstance = new puppeteerPDF();3let convertedValue = puppeteerPDFInstance.convertPrintParameterToInches('2cm');4const puppeteerPDF = require('puppeteer-pdf');5const puppeteerPDFInstance = new puppeteerPDF();6let convertedValue = puppeteerPDFInstance.convertPrintParameterToMillimeters('2cm');7const puppeteerPDF = require('puppeteer-pdf');8const puppeteerPDFInstance = new puppeteerPDF();9let convertedValue = puppeteerPDFInstance.convertPrintParameterToPoints('2cm');10const puppeteerPDF = require('puppeteer-pdf');11const puppeteerPDFInstance = new puppeteerPDF();12let convertedValue = puppeteerPDFInstance.convertPrintParameterToPixels('2cm');13const puppeteerPDF = require('puppeteer-pdf');14const puppeteerPDFInstance = new puppeteerPDF();15let convertedValue = puppeteerPDFInstance.convertPrintParameterToDots('2cm');16const puppeteerPDF = require('puppeteer-pdf');17const puppeteerPDFInstance = new puppeteerPDF();18let convertedValue = puppeteerPDFInstance.convertPrintParameterToCSSPixels('2cm');19const puppeteerPDF = require('puppeteer-pdf');20const puppeteerPDFInstance = new puppeteerPDF();21let convertedValue = puppeteerPDFInstance.convertPrintParameterToDisplayPixels('2cm');22const puppeteerPDF = require('puppeteer-pdf');

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const browser = await puppeteer.launch();3const page = await browser.newPage();4const inches = await page.evaluate(() => {5 return convertPrintParameterToInches('2in');6});7console.log(inches);8await browser.close();9const puppeteer = require('puppeteer');10const browser = await puppeteer.launch();11const page = await browser.newPage();12const inches = await page.evaluate(() => {13 return convertPrintParameterToInches('2in');14});15console.log(inches);16await browser.close();17 at Object.<anonymous> (/Users/.../test.js:7:21)18 at Module._compile (internal/modules/cjs/loader.js:778:30)19 at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)20 at Module.load (internal/modules/cjs/loader.js:653:32)21 at tryModuleLoad (internal/modules/cjs/loader.js:593:12)22 at Function.Module._load (internal/modules/cjs/loader.js:585:3)23 at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)24 at startup (internal/bootstrap/node.js:283:19)25 at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful