How can Puppeteer export a PDF from a print window?

Clicking a print button opens a dialog not tied to a direct URL. How can Puppeteer be used to save it as a PDF? See sample code below:

const puppeteerModule = require('puppeteer');

(async () => {
  const browserApp = await puppeteerModule.launch();
  const tab = await browserApp.newPage();
  await tab.setViewport({ width: 1400, height: 1800 });
  await tab.goto(`https://example.com/print?ref=${process.argv[2]}`, { waitUntil: 'domcontentloaded' });
  console.log(await tab.$eval('.info-text', el => el.textContent));
  await tab.click('.print-trigger');
  await tab.click('.pdf-option');
  await browserApp.close();
})();

hey, you cant interact with native print dialogz. instead, use page.pdf() on the content page after it loads. works good for me, so give that a try.

In my experience, handling the native print dialog isn’t straightforward. One viable alternative is to monitor the network requests after triggering the print functionality. For instance, if clicking the print button results in a network call that returns the PDF data, you can intercept that response and save the PDF manually. This method involves enabling request interception in Puppeteer and identifying the response with the appropriate content header. Although it requires additional coding compared to using page.pdf(), it has worked reliably in my projects.