Converting print dialog content to PDF with Puppeteer automation

I’m working on a web automation project where I need to click a print button that triggers a server request and displays a print dialog window. The challenge is that this content doesn’t come from a direct URL, so I can’t just navigate to it and use the standard PDF generation methods.

When I click the print button, it shows a browser print dialog with the document I need to save. I’m wondering if there’s a way to capture this dialog content and convert it to PDF using Puppeteer.

Here’s my current automation script:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const pageInstance = await browser.newPage();
  await pageInstance.setViewport({width: 1366, height: 1800});
  await pageInstance.goto(`https://www.southwest.com/air/check-in/review.html?confirmationNumber=${process.argv[2]}&passengerFirstName=${process.argv[3]}&passengerLastName=${process.argv[4]}`, {
    waitUntil: 'networkidle2'
  });
  
  const [confirmCode, buttonLabel] = [await pageInstance.$eval('.confirmation-number--code', element => element.innerHTML), await pageInstance.$eval('.actionable_primary.actionable_large-button', element => element.innerHTML)];
  
  console.log(buttonLabel);
  console.log(confirmCode);
  
  await pageInstance.click('.actionable_primary.actionable_large-button');
  await pageInstance.click('.air-check-in-boarding-pass-options .boarding-pass-options--button-email, .air-check-in-boarding-pass-options .boarding-pass-options--button-print, .air-check-in-boarding-pass-options .boarding-pass-options--button-text');
  
  await browser.close();
})();

Any suggestions on how to handle the print dialog and save the content as PDF would be really helpful.

Launch puppeteer with {headless: false} to see what’s actually happening when you click print. Sometimes content loads in a new tab or iframe - you’ll need to switch to it before generating the PDF. Also check if there’s a printPreview parameter you can add to the URL to skip the dialog completely.

I’ve hit similar issues. Skip trying to capture the dialog - intercept the print request instead. Use page.evaluateOnNewDocument() to override window.print() before the page loads. When someone clicks print, you’ll catch the formatted content and can run page.pdf() on it directly. You might need to listen for network responses after the print click too, since the content could load via XHR. The trick is grabbing that content after it’s formatted but before the browser sends it to the print system.

Skip the print dialog entirely and grab the content before it hits the browser’s print system. The dialog just shows what’s already in the DOM or pulled from your server request. After you click print, wait for everything to load, then use Puppeteer’s PDF generation. You’ll probably need waitForSelector or waitForResponse after your click to make sure the content fully loads. Once it’s rendered, just use page.pdf() instead of trying to mess with the system dialog. Way more control and you won’t deal with browser-specific dialog headaches.