If you want the words out of a PDF without uploading the file anywhere, the answer is one function: page.getTextContent() from Mozilla's pdf.js. And if you already build PDF tools with pdf-lib — the library from my merging post — here is the thing nobody tells you until you waste an afternoon on it: pdf-lib cannot extract text at all. Both "do PDFs in JavaScript", but they do opposite halves of the job, and picking the wrong one is the most common dead end in browser PDF work.
Below: what pdf.js does, the exact code already running in one of my production tools, and a real test on two real PDFs.
The trap: pdf-lib is the wrong library for this job
My PDF merger runs on pdf-lib, so when I needed text out of a PDF, I reached for the same library first. I checked the only way that settles it:
const { PDFDocument } = require('pdf-lib'); Object.getOwnPropertyNames(PDFDocument.prototype) .filter((m) => /text|extract/i.test(m)); // → [] — nothing. Zero methods. pdf-lib 1.17.1 exposes 46 methods on PDFDocument.prototype, and not one of them mentions text or extraction. It can load a document, copy pages between documents, draw new text onto a page, and save the result. Reading the text already in a page is simply not in its vocabulary. That is a design boundary, not a missing feature: pdf-lib is a document builder, and extraction belongs to a document reader.
What pdf.js actually does with your file
A PDF does not store text the way a .txt file does. It stores positioned fragments — a string here, a string there, each with coordinates — and the order those fragments sit in the file is not necessarily the order a person reads them. pdf.js, Mozilla's open-source PDF parser, parses that structure and hands you the fragments per page:
const page = await pdf.getPage(pageNumber); const content = await page.getTextContent(); const text = content.items .map((item) => item.str) .join(' '); getTextContent() returns one entry per text fragment, each with a str property. Join them and you have the page's words — the entire extraction step. Everything else in a real tool is interface: file picker, progress label, copy button, download-as-.txt button.
Two things worth knowing before you trust the output blindly. First, the join matters: the example joins fragments with a space, which is what my link extractor does. Nutrient's guide improves on this by checking each item's hasEOL flag and inserting a newline where the PDF ends a line — better for prose, where a space-join can glue a heading to the next paragraph. Second, pdf.js reads the text layer. A scanned PDF is pictures, not text, and getTextContent() will honestly return nothing for those pages. That is the boundary between extraction and OCR — a different tool (Tesseract.js) for a different problem.
The same code, already running in production
This is live code from ToolNest's PDF link extractor, running pdf.js in visitors' browsers — same library, same version, same pattern:
const PDFJS_URLS = [ "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.min.js", "https://unpkg.com/pdfjs-dist@3.11.174/build/pdf.min.js" ]; const PDFJS_WORKER = "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.worker.min.js"; Two details in there earned their place the hard way. The library loads from jsDelivr first and falls back to unpkg if the first CDN fails. And the worker script is pinned to the same version as the main script, because a version mismatch fails silently — the document just never loads.
The extraction loop is the heart of the tool, and it is also where the link extractor gets its text to scan:
const doc = await window.pdfjsLib.getDocument({data: bytes.slice(0)}).promise; for (let p = 1; p <= doc.numPages; p++) { const page = await doc.getPage(p); // ...annotations checked first... const tc = await page.getTextContent(); const text = tc.items.map(it => it.str).join(" "); // ...text scanned for URLs... page.cleanup(); } Notice what it does not do: the file bytes come from file.arrayBuffer() — the file the visitor picked, read locally. No upload, no endpoint, no server anywhere in the path. The browser parses the PDF in a Web Worker, and closing the tab leaves nothing behind — a contract never has to trust anyone's retention policy.
I ran it on two real PDFs — here are the numbers
I ran the same library version (pdfjs-dist 3.11.174) against two real files from my own site — Pagelyra's free sample PDFs. Test script: /tmp/pdftext-test/extract-test.js, Node v24.22.1, 2026-11-26, after npm install pdfjs-dist@3.11.174 pdf-lib:
| File | Size | Pages | Time | Text recovered |
|---|---|---|---|---|
reading-journal-sample.pdf | 4,879 bytes | 4 | 184 ms | 1,328 characters |
clockwork-garden-chapter-1.pdf | 5,819 bytes | 3 | 15 ms | 5,352 characters |
The journal sample is mostly form labels, returned exactly, blank lines included as __________ underscores. The chapter sample is prose, back in reading order, dialogue quotes intact: "Who is it for, Gran? The key." "Whoever the garden chooses."
Two honest notes. The 184 ms includes the library warming up — the second file took 15 ms, the honest per-file cost once loaded. Node also printed warnings about missing standard font data; they did not affect the output, and they are a Node-only artifact. The point is not the milliseconds: a four-page PDF gives up its text in well under a second, with no server, on a library version already proven in production.
Three things that will still bite you
Scanned PDFs return nothing. If the page is a photograph of a page, there is no text layer, and getTextContent() returns an empty list. Say so in your UI — "no text found, this looks like a scanned document" — instead of showing a blank box and letting the user assume the tool is broken.
Reading order is not guaranteed. pdf.js returns fragments in the order they sit in the file, which for multi-column layouts can mean the left column's second paragraph arrives after the right column's first. Single-column documents read correctly, as the two test files show. For two-column papers, expect interleaving — sort fragments by coordinates before joining.
The worker setup has two silent failure modes. If you forget to set workerSrc, pdf.js falls back to a "fake worker" on the main thread — extraction still works, but a 50-page PDF freezes the page while it parses. And if the worker version does not match the main library version, loading hangs with no error at all. Both are documented in the w3tweaks guide on PDF.js extraction — pin both scripts to one version string, as the production code does.
FAQ
Can pdf-lib extract text from a PDF?
No. I verified it directly: pdf-lib 1.17.1's PDFDocument has 46 prototype methods and none of them reads text. pdf-lib builds and modifies PDFs; extraction is pdf.js's job.
Does this work on scanned PDFs?
No — a scan has no text layer to read, so getTextContent() returns nothing for those pages. Scanned documents need OCR (Tesseract.js runs in the browser). Check whether you can select text in your PDF reader first: if you can select it, pdf.js can extract it.
How large a PDF can the browser handle?
No hard limit, but the whole file sits in memory, and a 5 MB PDF can expand well beyond its file size once parsed. Ordinary documents run in milliseconds, as the test above shows.
Is my file uploaded anywhere?
No. The file is read with file.arrayBuffer(), parsed by pdf.js in a Web Worker, and the text is produced in your tab. There is no server endpoint involved at all — the entire reason to build it this way.
The short version
PDF to text in the browser is a solved problem, but only if you pick the reading library: pdf.js, getDocument() then getTextContent() per page, worker pinned to the same version. pdf-lib is the wrong tool for this half of the job — and knowing that saves you the afternoon I spent learning it.
ToolNest's PDF link extractor already runs this exact code in production — it reads the same text layer to find URLs — and the PDF merger from my earlier post covers the other half with pdf-lib. Between the two libraries, the browser does the whole job, and your file never leaves your machine.
