PDF to Text Node.js Examples

Complete Node.js examples for common PDF to Text tasks. 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. Each Node.js example can be copied and run independently.

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

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.convertFileToFile("invoice.pdf", "invoice.txt", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Keep in memory

const pdfcrowd = require('pdfcrowd');

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;
        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");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    client.convertFile("invoice.pdf", {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("invoice.txt");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "invoice.txt");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} catch (error) {
    fail(error);
}

Convert a URL

Fetch a PDF from its URL.

Save to a file

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.convertUrlToFile(
    "https://your-server.com/invoice.pdf", "invoice.txt",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

Keep in memory

const pdfcrowd = require('pdfcrowd');

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;
        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");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    client.convertUrl("https://your-server.com/invoice.pdf", {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("invoice.txt");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "invoice.txt");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} 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

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

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) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Keep in memory

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

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;
        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");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer
    client.convertRawData(pdf_data_1, {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("invoice.txt");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "invoice.txt");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} catch (error) {
    fail(error);
}

Read from an input stream

Open invoice.pdf as a binary input stream and save the result to invoice.txt.

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

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) {
                console.error(error.toString());
                process.exitCode = 1;
                return;
            }
            console.log("Saved:", filePath);
        }
    );
} catch (error) {
    input_stream.destroy();
    throw error;
}

Decode extracted text

Convert a PDF URL, decode the returned bytes as UTF-8, and print the resulting string.

const pdfcrowd = require('pdfcrowd');

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;
        console.error(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode).toString());
    },
    end: () => {
        if (failed) {
            return;
        }
        const text_bytes = Buffer.concat(chunks); // Buffer
        const text = text_bytes.toString('utf8');
        console.log(text);
    }
});

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.

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.setPrintPageRange("1,3");
client.convertFileToFile("extraction.pdf", "excerpt.txt", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

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.

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.setNoLayout(true);
client.setPrintPageRange("1");
client.convertFileToFile("extraction.pdf", "article.txt", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

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.

const pdfcrowd = require('pdfcrowd');

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) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

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

const pdfcrowd = require('pdfcrowd');

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) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Extract an invoice field block

const pdfcrowd = require('pdfcrowd');

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) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

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

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.setPageBreakMode("default");
client.convertFileToFile("extraction.pdf", "pages.txt", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Use visible separators

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.PdfToTextClient("demo", "demo");

client.setCustomPageBreak(`
--- PAGE BREAK ---
`);
client.setPageBreakMode("custom");
client.convertFileToFile(
    "extraction.pdf", "separated.txt",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);