Image to PDF Node.js Examples

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

Basic examples
Express.js examples

Basic examples

PNG file to PDF file

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

PNG file to PDF file via callback

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

// 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/logo.png", callbacks);

PNG url to PDF file

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

PNG url to PDF file via callback

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

// 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/images/logo.png", callbacks);

In-memory PNG to PDF file

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

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

In-memory PNG to PDF file via callback

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

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

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

// 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/logo.png"), callbacks);

Get info about the current conversion

var pdfcrowd = require("pdfcrowd");

// create the API client instance
var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

// configure the conversion
try {
    client.setDebugLog(true);
} 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/logo.png",
    "logo.pdf",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
        
        // print URL of the debug log
        console.log("Debug log url: " + client.getDebugLogUrl());
        
        // print the number of conversion credits remaining in your account
        console.log("Remaining credit count: " + client.getRemainingCreditCount());
        
        // print the number of credits used for the conversion
        console.log("Consumed credit count: " + client.getConsumedCreditCount());
        
        // print the unique identifier for the conversion
        console.log("Job id: " + client.getJobId());
        
        // print size of the output data in bytes
        console.log("Output size: " + client.getOutputSize());
    });

Express.js examples

PNG file to PDF 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 the API client instance
    var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendGenericHttpResponse(
        res, "application/pdf", "logo.pdf", "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);
    }

    // run the conversion
    client.convertFile("/path/to/logo.png", callbacks);
});

module.exports = app;

PNG url to PDF 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 the API client instance
    var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendGenericHttpResponse(
        res, "application/pdf", "logo.pdf", "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);
    }

    // run the conversion
    client.convertUrl("https://pdfcrowd.com/static/images/logo.png", callbacks);
});

module.exports = app;

In-memory PNG to PDF 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 the API client instance
    var client = new pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

    // configure the callback to send a file in the HTTP response
    var callbacks = pdfcrowd.sendGenericHttpResponse(
        res, "application/pdf", "logo.pdf", "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);
    }

    // run the conversion
    client.convertRawData(fs.readFileSync("/path/to/logo.png"), callbacks);
});

module.exports = app;