PDF to PDF Node.js Examples
These examples cover common PDF 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.
Importing the client
Examples use ES modules. For CommonJS, replace the import
statements with require() and place awaited operations inside an
async function.
CommonJS
const pdfcrowd = require('pdfcrowd');
TypeScript
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.
Merging
Add PDFs in the order they should appear in the merged document. Choose file, in-memory, or output-stream handling for the result. The stream example uses a binary file stream as its destination.
Merge PDF files
Merge first.pdf and second.pdf in that order.
Save to a file
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); const filePath = await client.convertToFile("merged.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.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); client.convertToFile("merged.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)); }
Keep in memory
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); const pdf = await client.convert(); // 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.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); const chunks = []; let failed = false; client.convert({ 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)); }
Write to a stream
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; import { pipeline } from 'node:stream/promises'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); const stream = await client.convertToStream(); try { await pipeline(stream, fs.createWriteStream("merged.pdf")); } finally { stream.destroy(); } console.log("Saved:", "merged.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.PdfToPdfClient("demo", "demo"); client.addPdfFile("first.pdf"); client.addPdfFile("second.pdf"); client.convert({ data: (stream) => { if (failed) { stream.destroy(); return; } try { output_stream = fs.createWriteStream("merged.pdf"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "merged.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); }
Merge raw PDF bytes
Read first.pdf and second.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer const pdf_data_2 = fs.readFileSync("second.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfRawData(pdf_data_2); const filePath = await client.convertToFile("merged.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.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer const pdf_data_2 = fs.readFileSync("second.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfRawData(pdf_data_2); client.convertToFile("merged.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)); }
Keep in memory
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer const pdf_data_2 = fs.readFileSync("second.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfRawData(pdf_data_2); const pdf = await client.convert(); // 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.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer const pdf_data_2 = fs.readFileSync("second.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfRawData(pdf_data_2); const chunks = []; let failed = false; client.convert({ 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)); }
Merge bytes with a file
Add the bytes from first.pdf, followed by second.pdf, and save the result to merged.pdf.
import pdfcrowd from 'pdfcrowd'; import fs from 'node:fs'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfFile("second.pdf"); const filePath = await client.convertToFile("merged.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.PdfToPdfClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("first.pdf"); // Buffer client.addPdfRawData(pdf_data_1); client.addPdfFile("second.pdf"); client.convertToFile("merged.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)); }
PDF operations
Add a watermark
Apply watermark.pdf to proposal.pdf and save company_offer.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.setPageWatermark("watermark.pdf"); client.addPdfFile("proposal.pdf"); const filePath = await client.convertToFile("company_offer.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.PdfToPdfClient("demo", "demo"); client.setPageWatermark("watermark.pdf"); client.addPdfFile("proposal.pdf"); client.convertToFile("company_offer.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)); }
Linearize a PDF
Optimize not_linearized.pdf for progressive loading in a browser and save it as linearized.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.setLinearize(true); client.addPdfFile("not_linearized.pdf"); const filePath = await client.convertToFile("linearized.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.PdfToPdfClient("demo", "demo"); client.setLinearize(true); client.addPdfFile("not_linearized.pdf"); client.convertToFile("linearized.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 pages
Extract page 3 and pages 7 through the end of 13_pages.pdf into output.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.setAction("extract"); client.setPageRange("3,7-"); client.addPdfFile("13_pages.pdf"); const filePath = await client.convertToFile("output.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.PdfToPdfClient("demo", "demo"); client.setAction("extract"); client.setPageRange("3,7-"); client.addPdfFile("13_pages.pdf"); client.convertToFile("output.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)); }
Delete pages
Remove pages 1–3 and page 10 from 13_pages.pdf and save the remaining pages to output.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); client.setAction("delete"); client.setPageRange("1-3,10"); client.addPdfFile("13_pages.pdf"); const filePath = await client.convertToFile("output.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.PdfToPdfClient("demo", "demo"); client.setAction("delete"); client.setPageRange("1-3,10"); client.addPdfFile("13_pages.pdf"); client.convertToFile("output.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)); }
Split a PDF
Split 13_pages.pdf into two documents: pages 1–10 in pages1-10.pdf, and the remaining pages in pages11-end.pdf.
import pdfcrowd from 'pdfcrowd'; try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); async function convertPart(pageRange, output) { client.setPageRange(pageRange); const filePath = await client.convertToFile(output); console.log("Saved:", filePath); } client.addPdfFile("13_pages.pdf"); client.setAction('extract'); await convertPart('1-10', "pages1-10.pdf"); await convertPart('11-', "pages11-end.pdf"); } catch (error) { // Handle the error in your application. console.error(String(error)); }
import pdfcrowd from 'pdfcrowd'; function handleError(error) { // Handle the error in your application. console.error(String(error)); } try { const client = new pdfcrowd.PdfToPdfClient("demo", "demo"); function convertPart(pageRange, output, next) { try { client.setPageRange(pageRange); client.convertToFile(output, (error, filePath) => { if (error) { handleError(error); return; } console.log("Saved:", filePath); if (next) next(); }); } catch (error) { handleError(error); } } client.addPdfFile("13_pages.pdf"); client.setAction('extract'); convertPart('1-10', "pages1-10.pdf", () => { convertPart('11-', "pages11-end.pdf"); }); } catch (error) { handleError(error); }