RockBoro
RockBoro

Reputation: 2473

how to use puppeteer to goto web page then press Control P to print the page?

How to press control + P on a web page that is automated by puppeteer?

This code loads the web page. But using await page.keyboard.down('Control') to press the Control key has no effect.

(async () =>
{
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto(`https://google.com`);

  await page.waitForSelector('input'); 
  await page.focus("input");

 // this works
  await page.keyboard.down('Shift');
  await page.keyboard.press('KeyP');
  await page.keyboard.up('Shift');

// this has no effect.
  await page.keyboard.down('Control');
  await page.keyboard.press('KeyP');
  await page.keyboard.up('Control');

})();

What I would like to do is navigate to a PDF file. Have the browser open the PDF. Then press Control P and automate the print dialog to the extent that the code selects the printer to print to and presses the Enter key.

Upvotes: 1

Views: 2362

Answers (1)

RockBoro
RockBoro

Reputation: 2473

running puppeteer in kiosk mode enables the window.print( ) dialog to be automatically responded to.

const puppeteer = require('puppeteer');

(async () =>
{
  const browser = await puppeteer.launch(
    {
      headless: false, 
      "args": [ "--kiosk-printing" ]
    });

  const page = await browser.newPage();
  await page.goto(`file:///C:/Users/srich/Downloads/packing-list.pdf`);

  await page.evaluate(() => { window.print(); });
  await page.waitForTimeout(2000) ;

  await browser.close( ) ;
})();

Upvotes: 4

Related Questions