PDF to Text Node.js Examples

This page contains various examples of using the PDF to Text API in Node.js. The examples are complete and fully functional. Read more about how to convert PDF to Text in Node.js.

Basic examples

Basic examples

Convert a local PDF file to a text file

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// run the conversion and write the result to a file
client.convertFileToFile(
    "/path/to/invoice.pdf",
    "invoice.txt",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
    });

Convert a local PDF file to a text file via custom callbacks

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// use predefined callback for saving to a file
var callbacks = pdfcrowd.saveToFile("invoice.txt");

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    if(statusCode) {
        console.error("Pdfcrowd Error: " + statusCode + " - " + errMessage);
    } else {
        console.error("Pdfcrowd Error: " + errMessage);
    }
};

// run the conversion and write the result to a file
client.convertFile("/path/to/invoice.pdf", callbacks);

Convert url with PDF file to a text file

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// run the conversion and write the result to a file
client.convertUrlToFile(
    "https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf",
    "invoice.txt",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
    });

Convert url with PDF file to a text file via custom callbacks

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// use predefined callback for saving to a file
var callbacks = pdfcrowd.saveToFile("invoice.txt");

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    if(statusCode) {
        console.error("Pdfcrowd Error: " + statusCode + " - " + errMessage);
    } else {
        console.error("Pdfcrowd Error: " + errMessage);
    }
};

// run the conversion and write the result to a file
client.convertUrl("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf", callbacks);

Convert an in-memory PDF to a text file

var pdfcrowd = require("pdfcrowd");
var fs = require("fs");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// run the conversion and write the result to a file
client.convertRawDataToFile(
    fs.readFileSync("/path/to/hello_world.pdf"),
    "invoice.txt",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
    });

Convert an in-memory PDF to a text file via custom callbacks

var pdfcrowd = require("pdfcrowd");
var fs = require("fs");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// use predefined callback for saving to a file
var callbacks = pdfcrowd.saveToFile("invoice.txt");

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    if(statusCode) {
        console.error("Pdfcrowd Error: " + statusCode + " - " + errMessage);
    } else {
        console.error("Pdfcrowd Error: " + errMessage);
    }
};

// run the conversion and write the result to a file
client.convertRawData(fs.readFileSync("/path/to/hello_world.pdf"), callbacks);

Get info about the current conversion

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.PdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

// configure the conversion
try {
    client.setDebugLog(true);
    client.setPageBreakMode("default");
} catch(why) {
    // report the error
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

// run the conversion and write the result to a file
client.convertFileToFile(
    "/path/to/invoice.pdf",
    "invoice.txt",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
        
        // print URL to the debug log
        console.log("Debug log url: " + client.getDebugLogUrl());
        
        // print the number of available conversion credits in your account
        console.log("Remaining credit count: " + client.getRemainingCreditCount());
        
        // print the number of credits consumed by the conversion
        console.log("Consumed credit count: " + client.getConsumedCreditCount());
        
        // print the unique ID of the conversion
        console.log("Job id: " + client.getJobId());
        
        // print the total number of pages in the output document
        console.log("Page count: " + client.getPageCount());
        
        // print the size of the output in bytes
        console.log("Output size: " + client.getOutputSize());
    });

Advanced examples

Template rendering Examples