A utility for converting pdf to image formats with support for different outputs: directly to file, base64 or buffer.
Overall
score
97%
pdf2pic provides extensive configuration options for controlling output quality, dimensions, file handling, and graphics processing behavior.
Complete configuration interface for customizing PDF to image conversion behavior.
interface Options {
/** Image quality (0-100), where 0 is default quality */
quality?: number;
/** Output image format (png, jpg, jpeg, tiff, bmp, etc.) */
format?: string;
/** Output image width in pixels */
width?: number;
/** Output image height in pixels */
height?: number;
/** Whether to preserve original aspect ratio during resize */
preserveAspectRatio?: boolean;
/** Image resolution/DPI for conversion */
density?: number;
/** Directory path where image files will be saved */
savePath?: string;
/** Base filename for generated image files */
saveFilename?: string;
/** Compression algorithm for image output */
compression?: string;
/** Units for density measurement */
units?: 'Undefined' | 'PixelsPerInch' | 'PixelsPerCentimeter';
}pdf2pic ships with sensible defaults that work for most use cases:
const defaultOptions: Options = {
quality: 0, // Default quality (system default)
format: 'png', // PNG format for best quality
width: 768, // Standard web width
height: 512, // Proportional height
density: 72, // Standard screen resolution
preserveAspectRatio: false, // Force exact dimensions
savePath: './', // Current directory
saveFilename: 'untitled', // Generic filename
compression: 'jpeg', // JPEG compression algorithm
units: 'PixelsPerInch' // Standard DPI units
};Controls the output image quality and compression level.
quality?: number; // 0-100, where 0 uses system defaultUsage Examples:
import { fromPath } from "pdf2pic";
// Maximum quality (larger file sizes)
const highQuality = fromPath("/path/to/document.pdf", {
quality: 100,
format: "jpg"
});
// Balanced quality for web use
const webQuality = fromPath("/path/to/document.pdf", {
quality: 85,
format: "jpg"
});
// Use system default quality
const defaultQuality = fromPath("/path/to/document.pdf", {
quality: 0, // or omit this property
format: "png"
});Specifies the output image format. Supports all formats available in GraphicsMagick/ImageMagick.
format?: string; // Common: 'png', 'jpg', 'jpeg', 'tiff', 'bmp', 'gif', 'webp'Usage Examples:
import { fromPath } from "pdf2pic";
// PNG for documents with text (lossless)
const pngConvert = fromPath("/path/to/document.pdf", {
format: "png",
quality: 100
});
// JPEG for photos/images (smaller files)
const jpgConvert = fromPath("/path/to/document.pdf", {
format: "jpg",
quality: 90
});
// WebP for modern web browsers
const webpConvert = fromPath("/path/to/document.pdf", {
format: "webp",
quality: 80
});
// TIFF for archival/print purposes
const tiffConvert = fromPath("/path/to/document.pdf", {
format: "tiff",
compression: "lzw"
});Control output image dimensions and scaling behavior.
width?: number; // Output width in pixels
height?: number; // Output height in pixels
preserveAspectRatio?: boolean; // Maintain original proportionsUsage Examples:
import { fromPath } from "pdf2pic";
// Fixed dimensions (may distort if aspect ratio differs)
const fixedSize = fromPath("/path/to/document.pdf", {
width: 800,
height: 600,
preserveAspectRatio: false
});
// Preserve aspect ratio (fit within bounds)
const proportional = fromPath("/path/to/document.pdf", {
width: 800,
height: 600,
preserveAspectRatio: true
});
// Specify only width (height calculated automatically)
const widthOnly = fromPath("/path/to/document.pdf", {
width: 1200,
preserveAspectRatio: true
});
// High resolution for print
const printRes = fromPath("/path/to/document.pdf", {
width: 2400,
height: 3200,
density: 300,
preserveAspectRatio: true
});Control image resolution and processing quality settings.
density?: number; // DPI/PPI resolution
units?: 'Undefined' | 'PixelsPerInch' | 'PixelsPerCentimeter';Usage Examples:
import { fromPath } from "pdf2pic";
// Screen resolution (72 DPI)
const screenRes = fromPath("/path/to/document.pdf", {
density: 72,
units: "PixelsPerInch"
});
// Print resolution (300 DPI)
const printRes = fromPath("/path/to/document.pdf", {
density: 300,
units: "PixelsPerInch",
width: 2480, // A4 width at 300 DPI
height: 3508 // A4 height at 300 DPI
});
// High quality scanning (600 DPI)
const scanRes = fromPath("/path/to/document.pdf", {
density: 600,
units: "PixelsPerInch"
});
// Metric units
const metricRes = fromPath("/path/to/document.pdf", {
density: 118, // ~300 DPI in cm
units: "PixelsPerCentimeter"
});Configure where and how image files are saved.
savePath?: string; // Directory for output files
saveFilename?: string; // Base filename for output filesUsage Examples:
import { fromPath } from "pdf2pic";
// Organize by date
const dateOrganized = fromPath("/path/to/document.pdf", {
savePath: "./images/2023-12-01",
saveFilename: "document_page"
});
// Generates: ./images/2023-12-01/document_page.1.png
// Project-specific naming
const projectFiles = fromPath("/path/to/report.pdf", {
savePath: "./project/assets",
saveFilename: "quarterly_report"
});
// Generates: ./project/assets/quarterly_report.1.png
// Absolute paths
const absolutePath = fromPath("/path/to/document.pdf", {
savePath: "/var/www/uploads/images",
saveFilename: "user_document"
});
// Nested directories (ensure they exist)
import fs from "fs";
fs.mkdirSync("./output/thumbnails", { recursive: true });
const nested = fromPath("/path/to/document.pdf", {
savePath: "./output/thumbnails",
saveFilename: "thumb"
});Control image compression algorithms and quality trade-offs.
compression?: string; // Compression algorithm ('jpeg', 'lzw', 'zip', etc.)Usage Examples:
import { fromPath } from "pdf2pic";
// JPEG compression (good for photos)
const jpegComp = fromPath("/path/to/document.pdf", {
format: "jpg",
compression: "jpeg",
quality: 85
});
// LZW compression for TIFF (lossless)
const lzwComp = fromPath("/path/to/document.pdf", {
format: "tiff",
compression: "lzw"
});
// ZIP compression for PNG-like formats
const zipComp = fromPath("/path/to/document.pdf", {
format: "png",
compression: "zip"
});
// No compression (larger files, faster processing)
const noComp = fromPath("/path/to/document.pdf", {
format: "bmp",
compression: "none"
});Create reusable configuration profiles for different use cases:
import { fromPath, Options } from "pdf2pic";
// Predefined profiles
const profiles = {
thumbnail: {
width: 150,
height: 200,
format: "jpg",
quality: 75,
density: 72
} as Options,
web: {
width: 800,
height: 1000,
format: "webp",
quality: 85,
density: 96,
preserveAspectRatio: true
} as Options,
print: {
width: 2480,
height: 3508,
format: "tiff",
density: 300,
compression: "lzw",
preserveAspectRatio: true
} as Options,
archive: {
format: "png",
quality: 100,
density: 600,
compression: "zip"
} as Options
};
// Use profiles
const thumbConvert = fromPath("/path/to/document.pdf", profiles.thumbnail);
const webConvert = fromPath("/path/to/document.pdf", profiles.web);Modify configuration based on content or requirements:
import { fromPath } from "pdf2pic";
import fs from "fs";
const pdfPath = "/path/to/document.pdf";
const pdfStats = fs.statSync(pdfPath);
// Adjust quality based on file size
const options = {
format: "jpg",
width: 800,
quality: pdfStats.size > 10_000_000 ? 70 : 90, // Lower quality for large files
density: pdfStats.size > 50_000_000 ? 150 : 300 // Lower DPI for very large files
};
const convert = fromPath(pdfPath, options);import { fromPath } from "pdf2pic";
function createConverter(pdfPath: string, userOptions: Partial<Options>) {
// Validate and sanitize options
const options: Options = {
...userOptions,
width: Math.max(100, Math.min(4000, userOptions.width || 800)),
height: Math.max(100, Math.min(4000, userOptions.height || 600)),
quality: Math.max(0, Math.min(100, userOptions.quality || 85)),
density: Math.max(72, Math.min(600, userOptions.density || 150))
};
// Ensure save directory exists
if (options.savePath && !fs.existsSync(options.savePath)) {
fs.mkdirSync(options.savePath, { recursive: true });
}
return fromPath(pdfPath, options);
}Install with Tessl CLI
npx tessl i tessl/npm-pdf2picevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10