How to download a pdf file from chrome pdf preview using playwright
[Feature] PDF snapshot tests
Assert text from PDF on Print Preview modal
python - Playwright: Download via Print to PDF? - Stack Overflow
Videos
-> I faced the same problem earlier. I solved it by setting the attribute value of the download PDF button and then clicking on that button. -> You can check the sample code for more details
import { test } from '@playwright/test';
test.only('test', async ({ page }) => {
await page.route('**/*', route => {
if (route.request().url().startsWith("https://googleads.")) {
//console.log('Blocked ad request:', route.request().url());
route.abort();
} else {
route.continue();
}
});
// Navigate to the page containing the PDF link
await page.goto('https://www.sampledocs.in/BrowseFile/DummyFiles/pdf');
// Selector for the element to which you want to add a new attribute
const elementSelector = "a[href*='SampleDocs-sample-pdf']";
await page.evaluate((selector) => {
// Find the element using the provided selector
const element = document.querySelector(selector);
if (element) {
// By adding this new "download" attribute we can directly
//download the pdf instead of opening in preview
element.setAttribute('download','sample');
} else {
console.error('Element not found.');
}
}, elementSelector);
await page.waitForTimeout(1000);
const downloadPromise = page.waitForEvent('download');
await page.locator(elementSelector).first().click();
const download = await downloadPromise;
await download.saveAs(download.suggestedFilename());
});
I would use the waitForLoadState method in this to ensure the popup is fully loaded this can be done by adding
await popup.waitForLoadState('load') after you click the element to open it
https://playwright.dev/docs/api/class-page#page-wait-for-load-state
Additionally after PDF is generated and saved to a buffer you can save the buffer to a file with fs (https://www.geeksforgeeks.org/node-js-fs-writefilesync-method/) in a fashion like this I hope this helps and happy testing!
fs.writeFileSync('output.pdf', buffer)
Hi everyone, I'm trying to automate the Chrome Print Preview flow in Playwright and extract the content from the generated PDF, but I'm running into issues.
My Use Case:
-
I click on a button "Print Excuse Note", which opens Chrome's Print Preview.
-
The Print Preview does not open in a new tab (Playwright still sees only one page).
-
To save the PDF, I have to manually click the "Save" button inside the Print Preview UI.
-
I need to assert that the saved PDF contains expected text
Issues I'm Facing:
-
Playwright cannot detect or interact with Print Preview (it's not a new page or a regular modal).
-
page.pdf() only captures the current page's visible content, not the Print Preview document.
-
Trying to access print-preview-app via page.evaluate() results in "element not found", even though I can find it manually in the DevTools console.
-
The Print Preview UI appears to be part of the OS-level print dialog, making it inaccessible to Playwright.
Has anyone successfully automated Chrome's Print Preview with Playwright?
-
Is there a way to extract the text from the generated PDF automatically?
-
Any alternative approaches that worked for you?
Any help or insights would be greatly appreciated!
You don't actually need the print dialog, you can generate this directly from Playwright by emulating the media type.
page.emulate_media(media="print")
page.goto("https://robstarbuck.uk/cv");
page.pdf(path="cv.pdf")
This is how I generate my CV.
See also:
Playwright - Media Emulation
Playwright - PDF
Thanks very much to @KJ in the comments, who suggested that with headless=True, Chromium won't even put up a print dialog box in the first place.