You cannot pass a CSS selector to Puppeteer’s PDF method to print only that element. page.pdf() prints the page using print CSS; selector methods separately find or inspect DOM elements. To make a PDF of one selected fragment, first prepare a print view that contains that fragment, then call page.pdf(). For complex content, a dedicated print route or template is safer than modifying a live page.
What a CSS selector can—and cannot—do
Puppeteer’s page.$(), page.$$(), page.$eval(), and page.$$eval() methods let you find elements with CSS selectors. The PDF API is separate: page.pdf() generates a PDF of the page, not a selector-scoped fragment. The official guide puts it simply: “For printing PDFs use Page.pdf().” Puppeteer PDF generation guide · Page.pdf() API
That distinction determines the workflow: locate the element, make the intended content the page’s printable content, and then generate the PDF. A selector query alone does not alter what gets printed.
Choose how to prepare the selected content
Prefer a dedicated print route for production
If you control the site, create a print route or template that renders the report or other target content on its own, with the styles, assets, and page-break rules it needs. This avoids altering a complex application page at capture time and makes the print layout easier to maintain.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Use a print stylesheet when the original page is suitable
A stylesheet using @media print can hide unrelated page regions and show the selected content. Add the styles to the page before calling page.pdf(), or define them in the application. Print styles can also set page size and breaks. Check the result for fixed headers, overflow, and content that the application only renders after interaction.
Build a separate document for arbitrary fragments
When you do not own the page’s print layout, you can extract the selected element and render it in a new page. This is not automatically equivalent to the original: the fragment may depend on inherited styles, parent selectors, fonts, images, scripts, or page state. Recreate or load what it needs. For complicated pages, a manually maintained print view is more reliable than trying to transplant arbitrary markup.
Generate a PDF from a selected element
This example uses a dedicated print route, https://example.com/reports/quarterly/print, that renders the report by itself. Replace it with your own URL and selector. The selector check makes a missing report fail clearly rather than silently saving the wrong page.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto('https://example.com/reports/quarterly/print', {
waitUntil: 'networkidle0',
});
const selector = '.report';
const report = await page.$(selector);
if (!report) {
throw new Error(`No element matched ${selector}`);
}
await page.pdf({
path: 'quarterly-report.pdf',
format: 'A4',
printBackground: true,
preferCSSPageSize: true,
});
} finally {
await browser.close();
}
})();
Install Puppeteer in your project with npm install puppeteer, save the script as a JavaScript file, and run it with Node.js. The example waits for network activity to settle, but that is not a universal readiness rule: use a wait condition that fits how your page loads. If application data or images appear after network activity settles, explicitly wait for the relevant selector or application state before printing.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteRank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
The example checks that .report exists; the dedicated route is what ensures it is the printable content. On an ordinary page with navigation, sidebars, or other content, that check does not hide those regions. Add a print stylesheet or prepare a separate document before the PDF call if you need only the report.
Target one match or all matches
Choose the selector method based on whether your page should have one target or several:
page.$(selector)returns the first matching element, ornullif there is no match. It is useful when one element is expected and you want to check for its absence.page.$$(selector)returns all matching elements, or an empty array if none match. Use it to check how many items exist before preparing a print view.page.$eval(selector, fn)runsfnon the first match and throws if the selector matches nothing. It is handy for reading or changing one element.page.$$eval(selector, fn)passes all matching elements tofn, which is useful for collecting or transforming multiple items.
These query methods select or inspect DOM content; they do not limit page.pdf() to the selected element. See Puppeteer’s selector API reference, $$eval reference, and $eval reference.
Set print behavior deliberately
page.pdf() uses print CSS media. Its important options affect what appears and how pages are laid out:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
| Option | Documented behavior | When to set it |
|---|---|---|
path |
Optional output file path. If omitted, the method returns PDF bytes. | Set a path for a local file; use the returned bytes when sending or storing the PDF elsewhere. |
format |
Paper format defaults to Letter. | Set a paper size such as A4 when that is required by your output. |
margin |
No margins are set by default. | Set margins explicitly if the printed layout needs whitespace or a consistent printable area. |
printBackground |
Defaults to false. |
Set to true when background colors or graphics are part of the design. |
preferCSSPageSize |
Defaults to false. When true, CSS @page size takes priority over format, width, or height. |
Use it when the page’s print CSS defines the paper dimensions; otherwise choose the dimensions in the PDF options. |
waitForFonts |
Defaults to true; waits for document.fonts.ready. |
Usually leave the default in place. It does not ensure that application data or every image is ready. |
landscape, scale, pageRanges, headers and footers, dimensions, and timeout |
Available PDF options; exact behavior and supported values depend on the API version. | Consult the reference for the Puppeteer version used by your project. |
For PDF output using screen styles rather than print styles, call await page.emulateMediaType('screen') before page.pdf(). Do this only if the screen layout is actually the intended document; print media is the default. The current option definitions are in the PDFOptions API reference.
Wait for the right content before printing
Navigation completing does not necessarily mean the target is ready. A page may populate the report after an API request, lazy-load images, or wait for user interaction. Choose synchronization based on the page:
- Wait for a target element with Puppeteer’s selector waiting API when it appears after navigation.
- Wait for a known application-ready condition when data rendering continues after the element first appears.
- For long reports, check that lazy-loaded sections and images have been brought into view or otherwise loaded before PDF generation.
- Keep font loading in mind: PDF generation waits for fonts by default, but that is distinct from waiting for application content and images.
The PDF guide’s basic example navigates and then calls page.pdf(); it does not prescribe one universal navigation wait setting for every website. The PDF guide and API reference describe the print workflow and its options.
Common errors and practical fixes
The PDF still contains the whole page
The selector found an element, but it did not make that element the only printable content. Add or correct a print stylesheet, use a dedicated print route, or render the fragment in a separate document.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
The selector does not match
Check the selector spelling and whether the content exists at the time you query it. page.$() returns null for no match, while $eval() throws. Wait for the target if it is rendered asynchronously, and fail explicitly when it remains absent.
Colors or backgrounds are missing
PDF generation uses print media, which may have different styles from the screen view. Background printing is off by default; set printBackground: true if the design requires it. Also inspect the page’s print CSS for rules that intentionally remove backgrounds.
Fonts, images, or late data are missing
The default font wait covers document.fonts.ready, not every source of page readiness. Wait for the data and assets your selected content depends on, and check whether lazy-loaded material needs to be triggered before printing.
Paper size or layout is unexpected
Review format, width, height, margins, and CSS @page rules together. If CSS page sizing should win, set preferCSSPageSize: true; otherwise choose PDF dimensions explicitly. Check orientation, scaling, and page breaks in the resulting document.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Or skip the browser setup
If your goal is a page screenshot or PDF rather than a selector-scoped Puppeteer workflow, ScreenshotNeo is a website screenshot API and MCP server. Its API captures a URL; it does not turn Puppeteer’s selector query into a selector-scoped PDF call. One GET request can return a PDF, and it also supports selecting an element by CSS selector for capture.
For API parameters and options, see the ScreenshotNeo documentation. Example cURL request:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Cookie banners, newsletter popups, and chat widgets are removed before the shot; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and whether it was billed. An MCP server exposes take_screenshot, get_page_info, and capture_pdf to AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.
Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.
Frequently Asked Questions
Does Puppeteer have a `page.pdf(selector)` method?
No. The documented PDF method prints the page; selector querying and PDF generation are separate operations.
Can I use `page.$eval()` to make a PDF of just one element?
No. `$eval()` operates on the first matching element, but the PDF call still prints the page. Prepare a print view containing the intended content first.
Can I return PDF data without saving a file?
Yes. Omit `path` from `page.pdf()` and use the returned PDF bytes.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errors

