These examples cover common PDF to Image 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 ZIP archive. Stream examples use a binary file stream as their destination.
These basic examples produce a ZIP archive containing PNG images of the PDF pages. The archive variable holds ZIP bytes in the memory variants.
Upload an existing invoice.pdf file.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const filePath = await client.convertFileToFile( "invoice.pdf", "invoice.zip" ); 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertFileToFile( "invoice.pdf", "invoice.zip", (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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const archive = await client.convertFile("invoice.pdf"); // Buffer console.log("Archive ready:", archive.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const chunks = []; let failed = false; client.convertFile("invoice.pdf", { 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 archive = Buffer.concat(chunks); // Buffer console.log("Archive ready:", archive.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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const stream = await client.convertFileToStream("invoice.pdf"); try { await pipeline(stream, fs.createWriteStream("invoice.zip")); } finally { stream.destroy(); } console.log("Saved:", "invoice.zip"); } 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertFile("invoice.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.zip"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.zip"); }); } 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 a PDF from its URL.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const filePath = await client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.zip" ); 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.zip", (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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const archive = await client.convertUrl( "https://your-server.com/invoice.pdf" ); // Buffer console.log("Archive ready:", archive.length, "bytes"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const chunks = []; let failed = false; client.convertUrl("https://your-server.com/invoice.pdf", { 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 archive = Buffer.concat(chunks); // Buffer console.log("Archive ready:", archive.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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const stream = await client.convertUrlToStream( "https://your-server.com/invoice.pdf" ); try { await pipeline(stream, fs.createWriteStream("invoice.zip")); } finally { stream.destroy(); } console.log("Saved:", "invoice.zip"); } 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.zip"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.zip"); }); } 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 invoice.pdf 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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); const filePath = await client.convertRawDataToFile( pdf_data_1, "invoice.zip" ); 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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); client.convertRawDataToFile( pdf_data_1, "invoice.zip", (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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); const archive = await client.convertRawData(pdf_data_1); // Buffer console.log("Archive ready:", archive.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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); const chunks = []; let failed = false; client.convertRawData(pdf_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 archive = Buffer.concat(chunks); // Buffer console.log("Archive ready:", archive.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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); const stream = await client.convertRawDataToStream(pdf_data_1); try { await pipeline(stream, fs.createWriteStream("invoice.zip")); } finally { stream.destroy(); } console.log("Saved:", "invoice.zip"); } 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.PdfToImageClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("invoice.pdf"); // Buffer client.setForceZip(true); client.setOutputFormat("png"); client.convertRawData(pdf_data_1, { data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("invoice.zip"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.zip"); }); } 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 invoice.pdf as a binary input stream and save the result to invoice.zip.
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const input_stream = fs.createReadStream("invoice.pdf"); try { const filePath = await client.convertStreamToFile( input_stream, "invoice.zip" ); 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); const input_stream = fs.createReadStream("invoice.pdf"); try { client.convertStreamToFile( input_stream, "invoice.zip", (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 report.pdf, a three-page document with text, a chart, and vector graphics. The CropBox variant uses cropbox.pdf, a page with a predefined crop boundary.
Render page 1 of report.pdf directly as a PNG. Use 72 DPI for a compact preview or 300 DPI for more detail at higher pixel dimensions and file size. Selecting one page produces an image file without ZIP packaging.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setDpi(72); client.setOutputFormat("png"); client.setPrintPageRange("1"); const filePath = await client.convertFileToFile( "report.pdf", "preview.png" ); 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.PdfToImageClient("demo", "demo"); client.setDpi(72); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "preview.png", (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.PdfToImageClient("demo", "demo"); client.setDpi(300); client.setOutputFormat("png"); client.setPrintPageRange("1"); const filePath = await client.convertFileToFile( "report.pdf", "page-300dpi.png" ); 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.PdfToImageClient("demo", "demo"); client.setDpi(300); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile( "report.pdf", "page-300dpi.png", (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 pages 1 and 3 of report.pdf as PNG images in selected-pages.zip. Force ZIP packaging so the result stays an archive even if you later select just one page.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.setPrintPageRange("1,3"); const filePath = await client.convertFileToFile( "report.pdf", "selected-pages.zip" ); 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.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.setPrintPageRange("1,3"); client.convertFileToFile( "report.pdf", "selected-pages.zip", (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)); }
Convert page 1 of report.pdf into page.jpg for applications that need JPEG input. JPEG is lossy and is not always smaller than PNG, especially for pages dominated by text and flat graphics.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setOutputFormat("jpg"); client.setPrintPageRange("1"); const filePath = await client.convertFileToFile("report.pdf", "page.jpg"); 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.PdfToImageClient("demo", "demo"); client.setOutputFormat("jpg"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "page.jpg", (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 page 1 of report.pdf into chart.png, or honor the predefined CropBox in cropbox.pdf to remove its production margins.
The chart example fixes 144 DPI and uses pixel coordinates from the rendered page's top-left corner: X = 96, Y = 370, width = 1024, height = 384. Adjust the coordinates when changing the page layout or DPI. The CropBox variant uses the boundary already stored in the PDF.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setCropAreaHeight(384); client.setCropAreaWidth(1024); client.setCropAreaX(96); client.setCropAreaY(370); client.setDpi(144); client.setOutputFormat("png"); client.setPrintPageRange("1"); const filePath = await client.convertFileToFile("report.pdf", "chart.png"); 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.PdfToImageClient("demo", "demo"); client.setCropAreaHeight(384); client.setCropAreaWidth(1024); client.setCropAreaX(96); client.setCropAreaY(370); client.setDpi(144); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "chart.png", (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.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseCropbox(true); const filePath = await client.convertFileToFile( "cropbox.pdf", "cropped-page.png" ); 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.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseCropbox(true); client.convertFileToFile( "cropbox.pdf", "cropped-page.png", (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 page 1 of report.pdf into grayscale.png, replacing colors with shades of gray while preserving the page dimensions.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseGrayscale(true); const filePath = await client.convertFileToFile( "report.pdf", "grayscale.png" ); 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.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseGrayscale(true); client.convertFileToFile( "report.pdf", "grayscale.png", (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)); }