Skip to main content

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.

Note

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.

Quick reference

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.

Info

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 OCR or ensure the PDF contains embedded text.

no method named get_pdf_text

Likely cause. Built without the pdf feature.

Fix. Add features = ["pdf"] to the dependency.