Install pdf-img-convert
masterInstall the package using npm to begin converting PDFs to images in your JavaScript project.
npm install pdf-img-convertrepository·master·Indexed 41 days ago
https://github.com/ol-th/pdf-img-convert.jsA lightweight JavaScript library built on Mozilla's PDF.js that converts PDF documents into images (PNG, JPEG, or WebP) without relying on the filesystem or external binaries. Version 2.0.0 provides the asynchronous convert() function, which supports PDF URLs, local paths, Buffers, Uint8Arrays, and Base64 data, returning an array of image buffers or Base64 strings based on the conversion_config.
Install the package using npm to begin converting PDFs to images in your JavaScript project.
npm install pdf-img-convertYou can pass an optional conversion_config object as the second argument to convert() to customize the output.
Configuration Keys:
width (Number): Target width in pixels. If both width and height are provided, width takes precedence.height (Number): Target height in pixels.scale (Number): Viewport scale ratio. Defaults to 1.page_numbers (Array<number>): A 1-indexed list of specific pages to render (e.g., [1, 3]).base64 (Boolean): If true, the function returns an array of base64-encoded strings instead of Uint8Array objects.const config = {
width: 100,
height: 100,
scale: 2,
page_numbers: [1, 2, 3],
base64: true
};
const output = await pdf2img.convert('./example.pdf', config);You can control the resolution of the output images using scale, width, or height in the conversion_config object. The logic follows this priority:
width or height is provided, the function calculates a scale factor to match that specific dimension.width nor height is provided, the scale multiplier is applied to the original viewport.Note: If width or height are provided as values <= 0, the library logs an error and defaults to 100% scale.
To receive base64-encoded strings instead of raw binary data, set the base64 key to true in the configuration object. This is useful for web applications that need to display images directly in the browser or send them via JSON APIs.
import pdf2img from 'pdf-img-convert';
let config = {
base64: true,
scale: 2
};
async function processPDF() {
const pdf2img = await import("pdf-img-convert");
const output = await pdf2img.convert('./examples/example.pdf', config);
// output is an array of base64 strings
output.forEach((base64Data, index) => {
console.log(`Page ${index + 1} base64:`, base64Data);
});
}
processPDF();The core API is the asynchronous convert function. It accepts several input formats and returns a Promise that resolves to an Array of images.
Supported Input Formats:
https://example.com/sample.pdf)./sample.pdf)Buffer object containing PDF dataUint8Array object containing PDF dataDefault Output:
By default, the function returns an Array of Uint8Array objects, where each object represents a PNG image.
import pdf2img from 'pdf-img-convert';
// Example: converting a local file
const pdfArray = await pdf2img.convert('./sample.pdf');
// pdfArray is an Array of Uint8Array (PNG data)The convert function is the primary API for transforming PDF data into an array of images. It supports various input formats and allows for detailed configuration of the output.
The pdf parameter can be one of the following:
node-fetch).pdfData:pdf/[type];base64,[data].Buffer object.The conversion_config object allows you to customize the output:
page_numbers: An array of specific page numbers to convert (e.g., [1, 3]). If omitted, all pages are converted.scale: A numeric multiplier for the output size (default is 1.0).width: Target width in pixels. If provided, the scale is adjusted to match this width.height: Target height in pixels. If provided, the scale is adjusted to match this height.format: The output image format. Supported values are 'jpeg', 'webp', or 'png' (default).base64: A boolean. If true, the returned array contains Base64 encoded strings instead of Uint8Array buffers.import { convert } from 'pdf-img-convert';
// Example: Convert specific pages to JPEG base64 strings
const images = await convert('path/to/file.pdf', {
page_numbers: [1, 2],
format: 'jpeg',
base64: true,
scale: 2.0
});
// images is an array of base64 stringsThe following keys are available in the conversion_config object passed to convert():
{
page_numbers?: number[]; // Array of page indices (1-based)
scale?: number; // Multiplier for viewport
width?: number; // Target width in pixels
height?: number; // Target height in pixels
format?: 'jpeg' | 'webp' | 'png'; // Image format
base64?: boolean; // Return base64 strings instead of Uint8Array
}