pdf2thumbnail
v2.0.0
Published
Fast, flexible PDF thumbnail generation for Node.js — per-page images, merged strips, and ZIP archives.
Downloads
100
Maintainers
Readme
pdf2thumbnail

Fast, flexible PDF thumbnail generation for Node.js. Convert PDF pages into images, merge them into a single strip, and optionally archive everything as a ZIP.
Requirements
ImageMagick CLI tools must be installed on your system.
macOS (Homebrew):
brew install imagemagickLinux (apt):
sudo apt-get install imagemagickLinux (yum):
sudo yum -y install ImageMagickSupported Platforms
- Linux
- macOS
Installation
npm install pdf2thumbnailQuick Start
import {writeThumbnails, getTotalNumberOfPages} from 'pdf2thumbnail';
// Generate thumbnails for all pages
const result = await writeThumbnails('report.pdf', './thumbnails');
console.log(result.thumbnailPaths); // ['./thumbnails/report_1.jpg', './thumbnails/report_2.jpg', ...]
console.log(result.mergedPath); // './thumbnails/report.jpg'
// Get the total page count
const pages = await getTotalNumberOfPages('report.pdf');
console.log(pages); // 12API
getTotalNumberOfPages(pdfPathOrDataUrl)
Returns the total number of pages in a PDF document.
Parameters
| Name | Type | Description |
|------|------|-------------|
| pdfPathOrDataUrl | string | Path to a PDF file, or a PDF encoded as a DataURL string. |
Returns Promise<number>
import {getTotalNumberOfPages} from 'pdf2thumbnail';
// From a file path
const totalFromFile = await getTotalNumberOfPages('/docs/report.pdf');
// From a DataURL
const totalFromDataUrl = await getTotalNumberOfPages('data:application/pdf;base64,...');writeThumbnails(pdfPathOrDataUrl, outputDir, options?)
Generates thumbnail images for each page of a PDF document. Each page becomes an individual image. All thumbnails are also merged into a single vertically-stacked image. Optionally creates a ZIP archive.
Parameters
| Name | Type | Default | Description |
|------|------|---------|-------------|
| pdfPathOrDataUrl | string | | Path to a PDF file, or a DataURL string. |
| outputDir | string | | Directory where thumbnails will be saved. Created automatically if it doesn't exist. |
| options.width | number | 595 | Width of each thumbnail in pixels. Height is calculated automatically. |
| options.quality | number | 100 | Image quality (1-100). |
| options.format | string | 'jpg' | Output format (e.g. 'jpg', 'png'). |
| options.xDensity | number | 288 | Horizontal resolution in DPI for PDF rasterization. |
| options.yDensity | number | 288 | Vertical resolution in DPI for PDF rasterization. |
| options.start | number | | Starting page number (1-based). |
| options.end | number | | Ending page number (1-based). |
| options.archive | boolean | false | Generate a ZIP archive of all thumbnails. |
| options.background | string | '#fff' | Background color of the merged image. Accepts color names, hex, rgb(), etc. |
| options.offset | number | 0 | Spacing in pixels between thumbnails in the merged image. |
Returns Promise<ThumbnailResult>
interface ThumbnailResult {
thumbnailPaths: string[]; // Paths to individual page thumbnails
mergedPath: string; // Path to the merged image
archivePath?: string; // Path to the ZIP archive (when archive: true)
}Examples
import {writeThumbnails} from 'pdf2thumbnail';
// All pages with default settings
const result = await writeThumbnails('presentation.pdf', './output');
// Custom quality and format
await writeThumbnails('presentation.pdf', './output', {
width: 300,
quality: 80,
format: 'png',
});
// Specific page range (pages 2 through 5)
await writeThumbnails('presentation.pdf', './output', {
start: 2,
end: 5,
});
// With ZIP archive and custom merged image styling
const archived = await writeThumbnails('presentation.pdf', './output', {
archive: true,
background: '#f0f0f0',
offset: 10,
});
console.log(archived.archivePath); // './output.zip'Testing
npm testChangelog
See CHANGELOG.md for release history.
Author
shumatsumonobu
