Image to Image Node.js Examples

Complete Node.js examples for common Image to Image tasks. 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. Each Node.js example can be copied and run independently.

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

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("jpg");
client.convertFileToFile("logo.png", "logo.jpg", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Keep in memory

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("jpg");
const chunks = [];
let failed = false;
client.convertFile("logo.png", {
    data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)),
    error: (message, statusCode) => {
        failed = true;
        console.error(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode).toString());
    },
    end: () => {
        if (failed) {
            return;
        }
        const image = Buffer.concat(chunks); // Buffer
        console.log("Image ready:", image.length, "bytes");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    client.setOutputFormat("jpg");
    client.convertFile("logo.png", {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("logo.jpg");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "logo.jpg");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} catch (error) {
    fail(error);
}

Convert a URL

Fetch an image from its URL.

Save to a file

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("jpg");
client.convertUrlToFile(
    "https://your-server.com/logo.png", "logo.jpg",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

Keep in memory

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("jpg");
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;
        console.error(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode).toString());
    },
    end: () => {
        if (failed) {
            return;
        }
        const image = Buffer.concat(chunks); // Buffer
        console.log("Image ready:", image.length, "bytes");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    client.setOutputFormat("jpg");
    client.convertUrl("https://your-server.com/logo.png", {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("logo.jpg");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "logo.jpg");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} catch (error) {
    fail(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

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

const image_data_1 = fs.readFileSync("logo.png"); // Buffer
client.setOutputFormat("jpg");
client.convertRawDataToFile(image_data_1, "logo.jpg", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Keep in memory

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

const image_data_1 = fs.readFileSync("logo.png"); // Buffer
client.setOutputFormat("jpg");
const chunks = [];
let failed = false;
client.convertRawData(image_data_1, {
    data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)),
    error: (message, statusCode) => {
        failed = true;
        console.error(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode).toString());
    },
    end: () => {
        if (failed) {
            return;
        }
        const image = Buffer.concat(chunks); // Buffer
        console.log("Image ready:", image.length, "bytes");
    }
});

Write to a stream

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

let output_stream;
let failed = false;

function fail(error) {
    if (failed) return;
    failed = true;
    console.error(error.toString());
    process.exitCode = 1;
    if (output_stream) output_stream.destroy();
}

try {
    const image_data_1 = fs.readFileSync("logo.png"); // Buffer
    client.setOutputFormat("jpg");
    client.convertRawData(image_data_1, {
        data: (stream) => {
            if (failed) {
                stream.destroy();
                return;
            }
            output_stream = fs.createWriteStream("logo.jpg");
            pipeline(stream, output_stream, (error) => {
                if (error) {
                    fail(error);
                    return;
                }
                if (failed) return;
                console.log("Saved:", "logo.jpg");
            });
        },
        error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error(
            message, statusCode)),
        end: () => {
            console.log("Conversion response ended.");
        },
    });
} catch (error) {
    fail(error);
}

Read from an input stream

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

const pdfcrowd = require('pdfcrowd');
const fs = require('node:fs');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("jpg");
const input_stream = fs.createReadStream("logo.png");
try {
    client.convertStreamToFile(input_stream, "logo.jpg", (error, filePath) => {
        input_stream.destroy();
        if (error) {
            console.error(error.toString());
            process.exitCode = 1;
            return;
        }
        console.log("Saved:", filePath);
    });
} catch (error) {
    input_stream.destroy();
    throw 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.

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("png");
client.setResize("400x400");
client.convertFileToFile("scan.png", "thumbnail.png", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

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

const pdfcrowd = require('pdfcrowd');

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");
client.convertFileToFile("scan.png", "square.png", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Transparent padding

const pdfcrowd = require('pdfcrowd');

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");
client.convertFileToFile(
    "scan.png", "square-transparent.png",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

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

const pdfcrowd = require('pdfcrowd');

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");
client.convertFileToFile("scan.png", "chart.png", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Remove a uniform border

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("png");
client.setRemoveBorders(true);
client.convertFileToFile("scan.png", "trimmed.png", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});

Correct image rotation

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

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("png");
client.setRotate("90");
client.convertFileToFile(
    "scan-sideways.png", "upright.png",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

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.

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setCanvasBackgroundColor("EEF2F6");
client.setOutputFormat("jpg");
client.convertFileToFile(
    "transparent-logo.png", "logo-background.jpg",
    (error, filePath) => {
        if (error) {
            console.error(error.toString());
            return;
        }
        console.log("Saved:", filePath);
    }
);

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.

const pdfcrowd = require('pdfcrowd');

const client = new pdfcrowd.ImageToImageClient("demo", "demo");

client.setOutputFormat("webp");
client.convertFileToFile("scan.png", "scan.webp", (error, filePath) => {
    if (error) {
        console.error(error.toString());
        return;
    }
    console.log("Saved:", filePath);
});