Image to Image Node.js Examples

These examples cover common Image 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.

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.

Basic examples

Choose file, in-memory, or output-stream handling for the JPEG result. Stream examples use a binary file stream as their destination.

Convert an image file

Upload an existing logo.png file.

Save to a file

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const filePath = await client.convertFileToFile("logo.png", "logo.jpg");
    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.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const image = await client.convertFile("logo.png"); // Buffer
    console.log("Image ready:", image.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.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const stream = await client.convertFileToStream("logo.png");
    try {
        await pipeline(stream, fs.createWriteStream("logo.jpg"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "logo.jpg");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Convert a URL

Fetch an image from its URL.

Save to a file

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const filePath = await client.convertUrlToFile(
        "https://your-server.com/logo.png", "logo.jpg"
    );
    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.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const image = await client.convertUrl("https://your-server.com/logo.png"); // Buffer
    console.log("Image ready:", image.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.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const stream = await client.convertUrlToStream(
        "https://your-server.com/logo.png"
    );
    try {
        await pipeline(stream, fs.createWriteStream("logo.jpg"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "logo.jpg");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Convert raw image bytes

Read logo.png 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.ImageToImageClient("demo", "demo");
    
    const image_data_1 = fs.readFileSync("logo.png"); // Buffer
    client.setOutputFormat("jpg");
    const filePath = await client.convertRawDataToFile(
        image_data_1, "logo.jpg"
    );
    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.ImageToImageClient("demo", "demo");
    
    const image_data_1 = fs.readFileSync("logo.png"); // Buffer
    client.setOutputFormat("jpg");
    const image = await client.convertRawData(image_data_1); // Buffer
    console.log("Image ready:", image.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.ImageToImageClient("demo", "demo");
    
    const image_data_1 = fs.readFileSync("logo.png"); // Buffer
    client.setOutputFormat("jpg");
    const stream = await client.convertRawDataToStream(image_data_1);
    try {
        await pipeline(stream, fs.createWriteStream("logo.jpg"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "logo.jpg");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Read from an input stream

Open logo.png as a binary input stream and save the result to logo.jpg.

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("jpg");
    const input_stream = fs.createReadStream("logo.png");
    try {
        const filePath = await client.convertStreamToFile(
            input_stream, "logo.jpg"
        );
        console.log("Saved:", filePath);
    } finally {
        input_stream.destroy();
    }
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Transform the image

These examples use scan.png, a 1224 × 1584 pixel image with a chart and a uniform white border. Rotation uses scan-sideways.png; JPEG background replacement uses transparent-logo.png.

Resize an image

Fit scan.png within 400 × 400 pixels while preserving its aspect ratio. The result is a 309 × 400 pixel thumbnail.png. Use a square canvas below when you need an exact 400 × 400 result.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("png");
    client.setResize("400x400");
    const filePath = await client.convertFileToFile(
        "scan.png", "thumbnail.png"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Create a square thumbnail

Fit the whole image onto a 400 × 400 pixel canvas with either a pale blue background or transparent padding. Set 96 DPI so the px canvas dimensions match output pixels. The transparent variant preserves the original image's white background; transparency applies to the added canvas area.

Solid padding

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setCanvasBackgroundColor("EEF2F6");
    client.setCanvasHeight("400px");
    client.setCanvasWidth("400px");
    client.setDpi(96);
    client.setOutputFormat("png");
    client.setPrintCanvasMode("fit");
    const filePath = await client.convertFileToFile("scan.png", "square.png");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Transparent padding

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setCanvasBackgroundColor("00000000");
    client.setCanvasHeight("400px");
    client.setCanvasWidth("400px");
    client.setDpi(96);
    client.setOutputFormat("png");
    client.setPrintCanvasMode("fit");
    const filePath = await client.convertFileToFile(
        "scan.png", "square-transparent.png"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Crop or trim an image

Extract the chart from scan.png into chart.png, or remove the uniform outer border and save trimmed.png.

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 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.

Extract a selected region

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setCropAreaHeight("384px");
    client.setCropAreaWidth("1024px");
    client.setCropAreaX("96px");
    client.setCropAreaY("370px");
    client.setDpi(96);
    client.setOutputFormat("png");
    const filePath = await client.convertFileToFile("scan.png", "chart.png");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Remove a uniform border

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("png");
    client.setRemoveBorders(true);
    const filePath = await client.convertFileToFile("scan.png", "trimmed.png");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Correct image rotation

Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.png.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("png");
    client.setRotate("90");
    const filePath = await client.convertFileToFile(
        "scan-sideways.png", "upright.png"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Replace transparency when creating a JPEG

Convert transparent-logo.png to logo-background.jpg with a pale blue background. JPEG cannot retain transparency, so choose the background color explicitly.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setCanvasBackgroundColor("EEF2F6");
    client.setOutputFormat("jpg");
    const filePath = await client.convertFileToFile(
        "transparent-logo.png", "logo-background.jpg"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Convert an image to WebP

Convert scan.png to scan.webp at its original pixel dimensions. WebP uses lossy compression here, which can reduce file size at the cost of fine detail. Compare the result with your source image before choosing it for web delivery.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.ImageToImageClient("demo", "demo");
    
    client.setOutputFormat("webp");
    const filePath = await client.convertFileToFile("scan.png", "scan.webp");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}