PDF to Image / Node.js Examples

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

Basic examples
Express.js examples

Basic examples

PDF file to PNG file

var pdfcrowd = require("pdfcrowd");

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

PDF file to PNG file via callback

var pdfcrowd = require("pdfcrowd");

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    // use predefined helper class for errors
    const error = new pdfcrowd.Pdfcrowd.Error(errMessage, statusCode);
    console.error("PDFCrowd Error: " + error);
};

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

PDF url to PNG file

var pdfcrowd = require("pdfcrowd");

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

PDF url to PNG file via callback

var pdfcrowd = require("pdfcrowd");

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    // use predefined helper class for errors
    const error = new pdfcrowd.Pdfcrowd.Error(errMessage, statusCode);
    console.error("PDFCrowd Error: " + error);
};

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

In-memory PDF to PNG file

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

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

In-memory PDF to PNG file via callback

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

// Create an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

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

// set custom error callback
callbacks.error = function(errMessage, statusCode) {
    // use predefined helper class for errors
    const error = new pdfcrowd.Pdfcrowd.Error(errMessage, statusCode);
    console.error("PDFCrowd Error: " + error);
};

// Run the conversion and save 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 an API client instance.
var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.setOutputFormat("png");
    client.setDebugLog(true);
    client.setForceZip(true);
} catch(why) {
    // Report the error.
    console.error("PDFCrowd Error: " + why);
    process.exit(1);
}

// Run the conversion and save the result to a file.
client.convertFileToFile(
    "/path/to/invoice.pdf",
    "invoice.zip",
    function(err, fileName) {
        if (err) return console.error("PDFCrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
        
        // print URL pointing to the debug log for this request.
        console.log("Debug log url: " + client.getDebugLogUrl());
        
        // print Number of conversion credits remaining in your account.
        console.log("Remaining credit count: " + client.getRemainingCreditCount());
        
        // print Number of credits consumed for this conversion.
        console.log("Consumed credit count: " + client.getConsumedCreditCount());
        
        // print Unique identifier assigned to this conversion job.
        console.log("Job id: " + client.getJobId());
        
        // print Total number of pages in the output document.
        console.log("Page count: " + client.getPageCount());
        
        // print Size of the output data in bytes.
        console.log("Output size: " + client.getOutputSize());
    });

Express.js examples

PDF file to PNG in Express.js

"use strict";

const express = require("express");
const app = express();
const pdfcrowd = require("pdfcrowd");

// The recommended method is POST.
app.post("/", (req, res) => {
    // Create an API client instance.
    var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendImageInHttpResponse(
        res, "image/png", "invoice.zip", "attachment");

    // configure the callback to send an error in the HTTP response
    callbacks.error = function(errMessage, statusCode) {
        res.set('Content-Type', 'text/plain');
        res.status(statusCode || 400);
        res.send(errMessage);
    }

    // If unsure whether the input is a single-page file, enforce ZIP output.
    try {
        client.setOutputFormat("png");
        client.setForceZip(true);
    } catch(why) {
        if(why instanceof pdfcrowd.Pdfcrowd.Error) {
            callbacks.error(why.toString(), why.getStatusCode());
        } else {
            callbacks.error(why);
        }
        return;
    }

    // Run the conversion.
    client.convertFile("/path/to/invoice.pdf", callbacks);
});

module.exports = app;

PDF url to PNG in Express.js

"use strict";

const express = require("express");
const app = express();
const pdfcrowd = require("pdfcrowd");

// The recommended method is POST.
app.post("/", (req, res) => {
    // Create an API client instance.
    var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendImageInHttpResponse(
        res, "image/png", "invoice.zip", "attachment");

    // configure the callback to send an error in the HTTP response
    callbacks.error = function(errMessage, statusCode) {
        res.set('Content-Type', 'text/plain');
        res.status(statusCode || 400);
        res.send(errMessage);
    }

    // If unsure whether the input is a single-page file, enforce ZIP output.
    try {
        client.setOutputFormat("png");
        client.setForceZip(true);
    } catch(why) {
        if(why instanceof pdfcrowd.Pdfcrowd.Error) {
            callbacks.error(why.toString(), why.getStatusCode());
        } else {
            callbacks.error(why);
        }
        return;
    }

    // Run the conversion.
    client.convertUrl("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf", callbacks);
});

module.exports = app;

In-memory PDF to PNG in Express.js

"use strict";

const express = require("express");
const app = express();
const pdfcrowd = require("pdfcrowd");
var fs = require("fs");

// The recommended method is POST.
app.post("/", (req, res) => {
    // Create an API client instance.
    var client = new pdfcrowd.PdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendImageInHttpResponse(
        res, "image/png", "invoice.zip", "attachment");

    // configure the callback to send an error in the HTTP response
    callbacks.error = function(errMessage, statusCode) {
        res.set('Content-Type', 'text/plain');
        res.status(statusCode || 400);
        res.send(errMessage);
    }

    // If unsure whether the input is a single-page file, enforce ZIP output.
    try {
        client.setOutputFormat("png");
        client.setForceZip(true);
    } catch(why) {
        if(why instanceof pdfcrowd.Pdfcrowd.Error) {
            callbacks.error(why.toString(), why.getStatusCode());
        } else {
            callbacks.error(why);
        }
        return;
    }

    // Run the conversion.
    client.convertRawData(fs.readFileSync("/path/to/hello_world.pdf"), callbacks);
});

module.exports = app;