SeleniumBase for Rust can print pages to PDF and extract text from PDF files. This is useful for validating generated reports, invoices, receipts, and other printable documents.
Printing a page to PDF is always available. Reading the text of a PDF needs the
pdf Cargo feature, which pulls in the pdf-extract parser (about 34 crates).
The CLI package seleniumbase-rs-cli has it on by default. Built with
--no-default-features, sbrs pdf-text and sbrs assert-pdf-text exit with
status 2 and say how to get it.
save_as_pdf and print_to_pdf print a page to PDF (always available).
With feature pdf: get_pdf_text, assert_pdf_text, pdf::extract_text_from_bytes, pdf::extract_text_from_url (refuses downloads over 100 MB) and PdfReader::open.
What you will learn
- How to save a page as PDF.
- How to extract and assert text from PDF files, bytes and URLs.
Save a page as PDF#
print_to_pdf is the same call; both print with the page background and the
page’s own CSS page size (CDP Page.printToPDF).
Extract text from a PDF (feature pdf)#
These are plain functions; they read the file and need no browser.
Assert PDF contents (feature pdf)#
Bytes, URLs, pages and passwords (feature pdf)#
extract_text_from_url refuses downloads larger than
pdf::MAX_DOWNLOAD_BYTES (100 MB).
Use cases#
- Validate generated reports.
- Verify invoices and receipts.
- Archive page snapshots as searchable documents.
Troubleshooting#
PDF is blank
Likely cause. Page not fully loaded.
Fix. Wait for readyState === 'complete' before printing.
Text extraction fails
Likely cause. PDF is scanned/image-only.
Fix. Use OCROptical character recognition: reading text out of an image. or ensure the PDF contains embedded text.
print_to_pdf unsupported
Likely cause. Driver is Firefox.
Fix. Use Chromium-based browsers for CDP print-to-PDF.
no method named get_pdf_text
Likely cause. Built without the pdf feature.
Fix. Add features = ["pdf"] to the dependency.