PDF to Text Node.js Examples
These examples cover common PDF to Text tasks in server-side Node.js code. See the Node.js SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed.
Importing the client
Examples use ES modules. For CommonJS, replace the import
statements with require() and place awaited operations inside an
async function.
CommonJS
const pdfcrowd = require('pdfcrowd');
TypeScript
The package includes TypeScript declarations. TypeScript infers conversion results and callback parameters from the SDK types. Use a namespace import:
import * as pdfcrowd from 'pdfcrowd';
For TypeScript compiled as CommonJS, you can also use:
import pdfcrowd = require('pdfcrowd');
When compiling a default import to CommonJS, enable esModuleInterop.
If your project needs Node.js types, see TypeScript dependency setup.
Basic examples
Choose file, in-memory, or output-stream handling for the text result. Stream examples use a binary file stream as their destination.
The memory variants return UTF-8 bytes. Decode them when your application needs a text string.
Convert a PDF file
Upload an existing invoice.pdf file.
Save to a file
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const filePath = await client.convertFileToFile( "invoice.pdf", "invoice.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.convertFileToFile( "invoice.pdf", "invoice.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Keep in memory
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const text_bytes = await client.convertFile("invoice.pdf"); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const chunks = []; let failed = false; client.convertFile("invoice.pdf", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const text_bytes = Buffer.concat(chunks); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Write to a stream
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const stream = await client.convertFileToStream("invoice.pdf"); try { await pipeline(stream, fs.createWriteStream("invoice.txt")); } finally { stream.destroy(); } console.log("Saved:", "invoice.txt"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.convertFile("invoice.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.txt"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.txt"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Convert a URL
Fetch a PDF from its URL.
Save to a file
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const filePath = await client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Keep in memory
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const text_bytes = await client.convertUrl( "https://your-server.com/invoice.pdf" ); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const chunks = []; let failed = false; client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const text_bytes = Buffer.concat(chunks); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Write to a stream
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const stream = await client.convertUrlToStream( "https://your-server.com/invoice.pdf" ); try { await pipeline(stream, fs.createWriteStream("invoice.txt")); } finally { stream.destroy(); } console.log("Saved:", "invoice.txt"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.txt"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.txt"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Convert raw PDF bytes
Read invoice.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer const filePath = await client.convertRawDataToFile( pdf_data_1, "invoice.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.convertRawDataToFile( pdf_data_1, "invoice.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Keep in memory
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer const text_bytes = await client.convertRawData(pdf_data_1); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer const chunks = []; let failed = false; client.convertRawData(pdf_data_1, { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const text_bytes = Buffer.concat(chunks); // Buffer console.log("Text ready:", text_bytes.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Write to a stream
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer const stream = await client.convertRawDataToStream(pdf_data_1); try { await pipeline(stream, fs.createWriteStream("invoice.txt")); } finally { stream.destroy(); } console.log("Saved:", "invoice.txt"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.convertRawData(pdf_data_1, { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.txt"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.txt"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Read from an input stream
Open invoice.pdf as a binary input stream and save the result to invoice.txt.
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const input_stream = fs.createReadStream("invoice.pdf"); try { const filePath = await client.convertStreamToFile( input_stream, "invoice.txt" ); console.log("Saved:", filePath); } finally { input_stream.destroy(); } } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); const input_stream = fs.createReadStream("invoice.pdf"); try { client.convertStreamToFile( input_stream, "invoice.txt", (error, filePath) => { input_stream.destroy(); if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { input_stream.destroy(); throw error; } } catch (error) { // Handle the error in your application. console.error(String(error)); }
Decode extracted text
Convert a PDF URL, decode the returned bytes as UTF-8, and print the resulting string.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); function showText(text_bytes) { const text = text_bytes.toString('utf8'); console.log(text); } const text_bytes = await client.convertUrl("https://your-server.com/invoice.pdf"); showText(text_bytes); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; function handleError(error) { // Handle the error in your application. console.error(String(error)); } try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); function showText(text_bytes) { const text = text_bytes.toString('utf8'); console.log(text); } const chunks = []; let failed = false; client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => stream.on('data', (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; handleError(new pdfcrowd.Pdfcrowd.Error(message, statusCode)); }, end: () => { if (!failed) { showText(Buffer.concat(chunks)); } } }); } catch (error) { handleError(error); }
Customize text extraction
These examples upload extraction.pdf, a three-page document with a two-column article, wrapped paragraphs, and invoice fields.
Extract selected pages
Extract pages 1 and 3 of extraction.pdf into excerpt.txt.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setPrintPageRange("1,3"); const filePath = await client.convertFileToFile( "extraction.pdf", "excerpt.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setPrintPageRange("1,3"); client.convertFileToFile( "extraction.pdf", "excerpt.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Extract text without layout spacing
Extract the article on page 1 into article.txt without spacing that recreates the PDF's columns. For tables, the default layout mode usually preserves column alignment better.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setNoLayout(true); client.setPrintPageRange("1"); const filePath = await client.convertFileToFile( "extraction.pdf", "article.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setNoLayout(true); client.setPrintPageRange("1"); client.convertFileToFile( "extraction.pdf", "article.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Reconstruct wrapped paragraphs
Extract page 2 into paragraphs.txt, joining wrapped lines with paragraph detection and removing line-end hyphens. For example, infor- followed by mation becomes information. Paragraph detection depends on the document's layout.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setParagraphMode("bounding-box"); client.setPrintPageRange("2"); client.setRemoveHyphenation(true); const filePath = await client.convertFileToFile( "extraction.pdf", "paragraphs.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setParagraphMode("bounding-box"); client.setPrintPageRange("2"); client.setRemoveHyphenation(true); client.convertFileToFile( "extraction.pdf", "paragraphs.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Extract text from a page region
Crop away the headers and footers on every page, or extract only the customer, invoice, and reference fields on page 3.
The coordinates below match a 612 × 792 point (US Letter) page. Measure X and Y from the top-left corner; 72 points equal one inch. Adjust the regions for your PDF.
Exclude headers and footers
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCropAreaHeight(648); client.setCropAreaWidth(540); client.setCropAreaX(36); client.setCropAreaY(72); const filePath = await client.convertFileToFile( "extraction.pdf", "body.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCropAreaHeight(648); client.setCropAreaWidth(540); client.setCropAreaX(36); client.setCropAreaY(72); client.convertFileToFile( "extraction.pdf", "body.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Extract an invoice field block
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCropAreaHeight(70); client.setCropAreaWidth(300); client.setCropAreaX(36); client.setCropAreaY(290); client.setPrintPageRange("3"); const filePath = await client.convertFileToFile( "extraction.pdf", "fields.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCropAreaHeight(70); client.setCropAreaWidth(300); client.setCropAreaX(36); client.setCropAreaY(290); client.setPrintPageRange("3"); client.convertFileToFile( "extraction.pdf", "fields.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Keep page boundaries
The default output has no page separators. Insert form-feed characters (\f) for processing text by page, or add a visible --- PAGE BREAK --- marker on its own line.
Use form-feed separators
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setPageBreakMode("default"); const filePath = await client.convertFileToFile( "extraction.pdf", "pages.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setPageBreakMode("default"); client.convertFileToFile( "extraction.pdf", "pages.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Use visible separators
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCustomPageBreak(` --- PAGE BREAK --- `); client.setPageBreakMode("custom"); const filePath = await client.convertFileToFile( "extraction.pdf", "separated.txt" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToTextClient("demo", "demo"); client.setCustomPageBreak(` --- PAGE BREAK --- `); client.setPageBreakMode("custom"); client.convertFileToFile( "extraction.pdf", "separated.txt", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }