PDF to PDF in Node.js

This page describes how to join multiple PDF files or update PDF attributes in Node.js using the Pdfcrowd API. The API is easy to use and the integration takes only a few of lines of code.

Samples

Installation

Install the client library from npm
npm install pdfcrowd

We also offer other installation options.

Authentication

Authentication is needed in order to use the Pdfcrowd API. The credentials used for accessing the API are your Pdfcrowd username and the API key.

You can try out the API without registration using the following demo credentials:
  • Username: demo
  • API key: ce544b6ea52a5621fb9d55f8b542d14d

To get your personal API credentials, you can start a free API trial or buy the API license.

Examples

var pdfcrowd = require("pdfcrowd");

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

// 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) {
    // report the error
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

// run the conversion and write 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);
    });
var pdfcrowd = require("pdfcrowd");

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

// 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) {
    // 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 write the result to a file
client.convert(callbacks);
var pdfcrowd = require("pdfcrowd");
var fs = require("fs");

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

// 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) {
    // report the error
    console.error("Pdfcrowd Error: " + why);
    process.exit(1);
}

// run the conversion and write 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);
    });
var pdfcrowd = require("pdfcrowd");
var fs = require("fs");

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

// configure the conversion
try {
    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 write 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);
    });
var pdfcrowd = require("pdfcrowd");

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

// configure the conversion
try {
    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 write 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);
    });
var pdfcrowd = require("pdfcrowd");

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

// configure the conversion
try {
    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 write 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);
    });
var pdfcrowd = require("pdfcrowd");
var fs = require("fs");

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

// configure the conversion
try {
    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 write 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 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());
    });

Troubleshooting

API Method Reference

Refer for details to the API Method Reference.