Extract Text from PDF in JavaScript: pdf-parse vs pdfjs-dist
Compare pdf-parse and pdfjs-dist for extracting text from PDF files in Node.js and the browser. Covers multi-column layouts, Unicode text, and OCR for scanned PDFs.
Extracting structured text from PDFs is a core component of search indexing, invoice parsing, LLM retrieval-augmented generation (RAG), and data analysis. This guide shows how to extract raw text and layout data in JavaScript and Node.js.
Using pdf-parse in Node.js
pdf-parse is a lightweight Node.js library built on top of Mozilla's PDF.js:
npm install pdf-parse
import fs from 'fs';
import pdf from 'pdf-parse';
export async function extractPdfText(filePath: string) {
const dataBuffer = fs.readFileSync(filePath);
const data = await pdf(dataBuffer);
return {
pageCount: data.numpages,
text: data.text,
info: data.info,
metadata: data.metadata,
};
}
Download our structured 5MB PDF test file to test text extraction, page counts, and metadata inspection.
Download 5MB PDF Sample →Extracting Text in the Browser with PDF.js
Mozilla's pdfjs-dist allows client-side parsing without server roundtrips:
import * as pdfjsLib from 'pdfjs-dist';
// Set worker source
pdfjsLib.GlobalWorkerOptions.workerSrc = '//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
export async function extractTextInBrowser(file: File): Promise<string> {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
let fullText = '';
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const content = await page.getTextContent();
const pageText = content.items.map((item: any) => item.str).join(' ');
fullText += `--- Page ${i} ---\n${pageText}\n\n`;
}
return fullText;
}
Handling Text Extraction Gotchas
- Two-column layouts: Text extractors reading stream order may read across columns instead of down.
- Font encoding: Non-standard embedded font encodings can output garbled Unicode characters without appropriate character map tables.
Nguyen Dai Long
AuthorBackend Lead • Distributed Systems & Cloud Edge Architecture Specialist
4+ years designing high-throughput file ingestion pipelines, database architectures, and distributed edge storage on Cloudflare R2 & AWS S3. Founder of FileDummy and the NDL Ecosystem.
Was this article helpful?
Click Like to support the author and help other developers discover this guide.
Engineering Discussion & Feedback (0)
Share benchmark results, report edge cases, or ask technical questions.
No comments yet. Be the first developer to start the discussion!
Frequently Asked Questions
What is the difference between pdf-parse and pdfjs-dist?â–¾
pdf-parse is a lightweight wrapper returning all text as a single string. pdfjs-dist provides lower-level access to individual text items with their coordinates, enabling layout-aware extraction for columns and tables. Use pdf-parse for simple extraction, pdfjs-dist when you need positional data.
Why is extracted text garbled or in wrong order?â–¾
PDFs store text as glyphs at absolute positions with no inherent reading order. Correct extraction requires sorting by Y position (lines) then X position (left-to-right). For multi-column PDFs, cluster glyphs into columns first using pdfjs-dist's TextContent API.
How do I extract text from a scanned PDF?â–¾
Scanned PDFs contain images, not text, so OCR is required. Use Tesseract.js in Node.js: render each PDF page to canvas with pdfjs-dist, then pass the canvas image data to Tesseract.