These examples cover common Image to PDF tasks in server-side Node.js code. See the Node.js SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed.
Examples use ES modules. For CommonJS, replace the import
statements with require() and place awaited operations inside an
async function.
const pdfcrowd = require('pdfcrowd');
The package includes TypeScript declarations. TypeScript infers conversion results and callback parameters from the SDK types. Use a namespace import:
import * as pdfcrowd from 'pdfcrowd';
For TypeScript compiled as CommonJS, you can also use:
import pdfcrowd = require('pdfcrowd');
When compiling a default import to CommonJS, enable esModuleInterop.
If your project needs Node.js types, see TypeScript dependency setup.
Choose file, in-memory, or output-stream handling for the PDF result. Stream examples use a binary file stream as their destination.
Upload an existing logo.png file.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const filePath = await client.convertFileToFile("logo.png", "logo.pdf"); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertFileToFile("logo.png", "logo.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const pdf = await client.convertFile("logo.png"); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const chunks = []; let failed = false; client.convertFile("logo.png", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const pdf = Buffer.concat(chunks); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const stream = await client.convertFileToStream("logo.png"); try { await pipeline(stream, fs.createWriteStream("logo.pdf")); } finally { stream.destroy(); } console.log("Saved:", "logo.pdf"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertFile("logo.png", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("logo.pdf"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "logo.pdf"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Fetch an image from its URL.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const filePath = await client.convertUrlToFile( "https://your-server.com/logo.png", "logo.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertUrlToFile( "https://your-server.com/logo.png", "logo.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const pdf = await client.convertUrl("https://your-server.com/logo.png"); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const chunks = []; let failed = false; client.convertUrl("https://your-server.com/logo.png", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const pdf = Buffer.concat(chunks); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const stream = await client.convertUrlToStream( "https://your-server.com/logo.png" ); try { await pipeline(stream, fs.createWriteStream("logo.pdf")); } finally { stream.destroy(); } console.log("Saved:", "logo.pdf"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertUrl("https://your-server.com/logo.png", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("logo.pdf"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "logo.pdf"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer const filePath = await client.convertRawDataToFile( image_data_1, "logo.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer client.convertRawDataToFile(image_data_1, "logo.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer const pdf = await client.convertRawData(image_data_1); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer const chunks = []; let failed = false; client.convertRawData(image_data_1, { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; // Handle the error in your application. console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const pdf = Buffer.concat(chunks); // Buffer console.log("PDF ready:", pdf.length, "bytes"); } }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer const stream = await client.convertRawDataToStream(image_data_1); try { await pipeline(stream, fs.createWriteStream("logo.pdf")); } finally { stream.destroy(); } console.log("Saved:", "logo.pdf"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; // Handle the error in your application. console.error(String(error)); if (output_stream) output_stream.destroy(); } try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const image_data_1 = fs.readFileSync("logo.png"); // Buffer client.convertRawData(image_data_1, { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("logo.pdf"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "logo.pdf"); }); } catch (error) { stream.destroy(); fail(error); } }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { // pipeline reports completion after the destination closes. }, }); } catch (error) { fail(error); }
Open logo.png as a binary input stream and save the result to logo.pdf.
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const input_stream = fs.createReadStream("logo.png"); try { const filePath = await client.convertStreamToFile( input_stream, "logo.pdf" ); console.log("Saved:", filePath); } finally { input_stream.destroy(); } } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); const input_stream = fs.createReadStream("logo.png"); try { client.convertStreamToFile(input_stream, "logo.pdf", (error, filePath) => { input_stream.destroy(); if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { input_stream.destroy(); throw error; } } catch (error) { // Handle the error in your application. console.error(String(error)); }
These examples use scan.png, a 1224 × 1584 pixel image with text, a chart, and a uniform white border. The rotation example uses its sideways variant, scan-sideways.png.
Fit the whole image onto A4 portrait or Letter landscape paper with 12mm margins and 150 DPI. The fit mode preserves the image's aspect ratio. Setting a page size without it can clip a larger image.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); const filePath = await client.convertFileToFile("scan.png", "a4.pdf"); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "a4.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setOrientation("landscape"); client.setPageSize("Letter"); client.setPrintPageMode("fit"); const filePath = await client.convertFileToFile( "scan.png", "landscape.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setOrientation("landscape"); client.setPageSize("Letter"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "landscape.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Fit scan.png onto a 4 × 6 inch page and save custom-page.pdf. Preserve the aspect ratio and fill the unused page area with a pale blue background.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setPageBackgroundColor("EEF2F6"); client.setPageHeight("6in"); client.setPageWidth("4in"); client.setPrintPageMode("fit"); const filePath = await client.convertFileToFile( "scan.png", "custom-page.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setPageBackgroundColor("EEF2F6"); client.setPageHeight("6in"); client.setPageWidth("4in"); client.setPrintPageMode("fit"); client.convertFileToFile( "scan.png", "custom-page.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Interpret scan.png at 300 pixels per inch without setting a fixed page size. Its 1224 × 1584 pixels produce a 4.08 × 5.28 inch page in print-size.pdf, with the original image pixels retained.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(300); const filePath = await client.convertFileToFile( "scan.png", "print-size.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(300); client.convertFileToFile( "scan.png", "print-size.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Extract the chart from scan.png into chart.pdf, or remove its uniform outer border and save trimmed.pdf.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match the source image pixels; adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setCropAreaHeight("384px"); client.setCropAreaWidth("1024px"); client.setCropAreaX("96px"); client.setCropAreaY("370px"); client.setDpi(96); const filePath = await client.convertFileToFile("scan.png", "chart.pdf"); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setCropAreaHeight("384px"); client.setCropAreaWidth("1024px"); client.setCropAreaX("96px"); client.setCropAreaY("370px"); client.setDpi(96); client.convertFileToFile("scan.png", "chart.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRemoveBorders(true); const filePath = await client.convertFileToFile("scan.png", "trimmed.pdf"); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRemoveBorders(true); client.convertFileToFile("scan.png", "trimmed.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRotate("90"); const filePath = await client.convertFileToFile( "scan-sideways.png", "upright.pdf" ); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRotate("90"); client.convertFileToFile( "scan-sideways.png", "upright.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); } ); } catch (error) { // Handle the error in your application. console.error(String(error)); }
Render scan.png onto A4 paper at 72 DPI and save compact.pdf. Keep the same fit and 12mm margins as the 150 DPI A4 example above, while reducing resolution for screen viewing. Smaller files come at the cost of fine detail.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(72); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); const filePath = await client.convertFileToFile("scan.png", "compact.pdf"); console.log("Saved:", filePath); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(72); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "compact.pdf", (error, filePath) => { if (error) { // Handle the error in your application. console.error(error.toString()); return; } console.log("Saved:", filePath); }); } catch (error) { // Handle the error in your application. console.error(String(error)); }