Extracting text from PDF URLs page by page in Node (no Python, no native deps) A developer published a Node 20 script that extracts per-page text from PDF URLs using Mozilla's pdf.js via the unpdf package, avoiding Python, native dependencies, or a canvas build step. The script validates downloads by checking for the %PDF magic bytes in the first 1 KB rather than trusting the URL, rebuilds lines from pdf.js text runs using the hasEOL flag, caps files at 50 MB with a 60-second timeout, and flags likely-scanned documents when word counts fall below roughly two per page. AI disclosure: I wrote this article with AI assistance Claude . The script is a trimmed-down version of an extractor we maintain, and every output below comes from running it on 2026-11-23. We reviewed and tested everything. If you're feeding documents into search, RAG or a spreadsheet, sooner or later someone hands you a list of PDF links. The usual advice is "use PyMuPDF" or "spin up Apache Tika". Both are fine, but if the rest of your pipeline is JavaScript, you can stay in Node. Mozilla's pdf.js already ships a solid text extractor, and the unpdf package wraps it so it runs in plain Node 20 with no canvas or native build step. This post builds a small script that: Joining everything into one string is tempting. Keep the pages anyway: npm i unpdf js // pdf-text.mjs: per-page text from PDF URLs. Node 20+, npm i unpdf . import { getDocumentProxy, getMeta } from 'unpdf'; const MAX BYTES = 50 1024 1024; async function download url { const res = await fetch url, { headers: { accept: 'application/pdf, / ;q=0.8' }, signal: AbortSignal.timeout 60 000 , } ; if res.ok throw new Error HTTP ${res.status} ; const buf = new Uint8Array await res.arrayBuffer ; if buf.length MAX BYTES throw new Error 'too large' ; // Many "PDF links" return an HTML login or cookie page. Check the magic bytes. const head = new TextDecoder 'latin1' .decode buf.subarray 0, 1024 ; if head.includes '%PDF' { throw new Error not a PDF content-type: ${res.headers.get 'content-type' } ; } return buf; } // pdf.js gives positioned text runs, not lines. Rebuild lines from hasEOL. function itemsToText items { let out = ''; for const it of items { if typeof it.str == 'string' continue; // marked-content items out += it.str; if it.hasEOL out += '\n'; else if it.str && out.endsWith ' ' out += ' '; } return out .replace / \t +\n/g, '\n' .replace / \t {2,}/g, ' ' .replace /\n{3,}/g, '\n\n' .trim ; } const words = s = s.match /\S+/g || .length; export async function extract url, maxPages = 0 { const pdf = await getDocumentProxy await download url ; const total = pdf.numPages; const limit = maxPages 0 ? Math.min total, maxPages : total; const pages = ; for let n = 1; n <= limit; n++ { const page = await pdf.getPage n ; const { items } = await page.getTextContent ; const text = itemsToText items ; pages.push { page: n, words: words text , text } ; page.cleanup ; // free per-page memory on big files } let info = {}; try { info = await getMeta pdf .info || {}; } catch {} const totalWords = pages.reduce a, p = a + p.words, 0 ; return { url, totalPages: total, pagesRead: pages.length, totalWords, title: info.Title || null, producer: info.Producer || null, // No text layer: fewer than ~2 words per page means a scan. likelyScanned: totalWords < pages.length 2, pages, }; } // CLI: node pdf-text.mjs