PDF to PDF / Node.js Examples

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

Basic examples
PDF manipulation examples
Express.js examples

Basic examples

Multiple PDFs to PDF file

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/path/to/cover.pdf");
    client.addPdfFile("/path/to/proposal.pdf");
    client.addPdfFile("/path/to/price.pdf");
    client.addPdfFile("/path/to/contact.pdf");
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Multiple PDFs to PDF file via callback

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/path/to/cover.pdf");
    client.addPdfFile("/path/to/proposal.pdf");
    client.addPdfFile("/path/to/price.pdf");
    client.addPdfFile("/path/to/contact.pdf");
} 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("offer.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 save the result to a file.
client.convert(callbacks);

Multiple in-memory PDFs to PDF file

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

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

try {
    // Configure the conversion.
    client.addPdfRawData(fs.readFileSync("/path/to/cover.pdf"));
    client.addPdfRawData(fs.readFileSync("/path/to/proposal.pdf"));
    client.addPdfRawData(fs.readFileSync("/path/to/price.pdf"));
    client.addPdfRawData(fs.readFileSync("/path/to/contact.pdf"));
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Join 2 in-memory PDFs together with 2 local PDF files to PDF file

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

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

try {
    // Configure the conversion.
    client.addPdfRawData(fs.readFileSync("/path/to/cover.pdf"));
    client.addPdfFile("/path/to/proposal.pdf");
    client.addPdfRawData(fs.readFileSync("/path/to/price.pdf"));
    client.addPdfFile("/path/to/contact.pdf");
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Watermark a PDF file

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/path/to/proposal.pdf");
    client.setPageWatermark("/path/to/watermark.pdf");
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Linearize a PDF file

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/path/to/not_linearized.pdf");
    client.setLinearize(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.convertToFile(
    "linearized.pdf",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
    });

Get info about the current conversion

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

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

try {
    // Configure the conversion.
    client.setDebugLog(true);
    client.addPdfRawData(fs.readFileSync("/path/to/cover.pdf"));
    client.addPdfRawData(fs.readFileSync("/path/to/proposal.pdf"));
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

// Run the conversion and save the result to a file.
client.convertToFile(
    "offer.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 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());
    });

PDF manipulation examples

Extract page 3 and all pages from 7 to the end from the PDF file

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    client.setAction("extract");
    client.setPageRange("3,7-");
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Delete the first 3 pages and the 10th page from the PDF file

var pdfcrowd = require("pdfcrowd");

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

try {
    // Configure the conversion.
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    client.setAction("delete");
    client.setPageRange("1-3,10");
} catch(why) {
    // Report the error.
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

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

Split the PDF file into two separate files at the 10th page

var pdfcrowd = require("pdfcrowd");
var client = new pdfcrowd.PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

try {
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    client.setAction("extract");
    client.setPageRange("1-10");
    client.convertToFile("pages1-10.pdf");
    client.setPageRange("11-");
} catch(why) {
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}
client.convertToFile(
    "pages11-end.pdf",
    function(err, fileName) {
        if (err) return console.error("Pdfcrowd Error: " + err);
        console.log("Success: the file was created " + fileName);
    });

Express.js examples

Multiple PDFs 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 an API client instance.
    var client = new pdfcrowd.PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

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

    // Configure the conversion.
    try {
        client.addPdfFile("/path/to/cover.pdf");
        client.addPdfFile("/path/to/proposal.pdf");
        client.addPdfFile("/path/to/price.pdf");
        client.addPdfFile("/path/to/contact.pdf");
    } catch(why) {
        if(why instanceof pdfcrowd.Pdfcrowd.Error) {
            callbacks.error(why.getMessage(), why.getCode());
        } else {
            callbacks.error(why);
        }
        return;
    }

    // Run the conversion.
    client.convert(callbacks);
});

module.exports = app;

Multiple in-memory PDFs 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 an API client instance.
    var client = new pdfcrowd.PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

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

    // Configure the conversion.
    try {
        client.addPdfRawData(fs.readFileSync("/path/to/cover.pdf"));
        client.addPdfRawData(fs.readFileSync("/path/to/proposal.pdf"));
        client.addPdfRawData(fs.readFileSync("/path/to/price.pdf"));
        client.addPdfRawData(fs.readFileSync("/path/to/contact.pdf"));
    } catch(why) {
        if(why instanceof pdfcrowd.Pdfcrowd.Error) {
            callbacks.error(why.getMessage(), why.getCode());
        } else {
            callbacks.error(why);
        }
        return;
    }

    // Run the conversion.
    client.convert(callbacks);
});

module.exports = app;