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
- PNG file to PDF file
- PNG file to PDF file via callback
- PNG url to PDF file
- PNG url to PDF file via callback
- In-memory PNG to PDF file
- In-memory PNG to PDF file via callback
- Get info about the current conversion
Express.js examples
Basic examples
PNG file to PDF file
var pdfcrowd = require("pdfcrowd"); // Create an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // Run the conversion and save 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // use predefined callback for saving to a file var callbacks = pdfcrowd.saveToFile("logo.pdf"); // 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/logo.png", callbacks);
PNG url to PDF file
var pdfcrowd = require("pdfcrowd"); // Create an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // Run the conversion and save 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // use predefined callback for saving to a file var callbacks = pdfcrowd.saveToFile("logo.pdf"); // 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/images/logo.png", callbacks);
In-memory PNG to PDF file
var pdfcrowd = require("pdfcrowd"); var fs = require("fs"); // Create an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // Run the conversion and save 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // use predefined callback for saving to a file var callbacks = pdfcrowd.saveToFile("logo.pdf"); // 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/logo.png"), callbacks);
Get info about the current conversion
var pdfcrowd = require("pdfcrowd"); // Create an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); try { // Configure the conversion. client.setDebugLog(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/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 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 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // 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 an API client instance. var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); // 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;