Generate PDF thumbnails.
You can install this module using npm:
npm i pdf2thumbnail
Requires imagemagick CLI tools to be installed.
There are numerous ways to install them.
For instance, if you're on OS X you can use Homebrew.
brew install imagemagick
For Linux, use yum.
sudo yum -y install ImageMagick
- Total pages of PDF.
const {getTotalNumberOfPages} = require('pdf2thumbnail'); const totalNumberOfPages = await getTotalNumberOfPages('sample.pdf'); console.log(totalNumberOfPages);// =>29
- Thumbnail all pages.
The thumbnail file name is "<original file name>_<page number>.<extension>".const {writeThumbnails} = require('pdf2thumbnail'); writeThumbnails('sample.pdf', './dir'); // $ ll dir // -rw-rw-r-- 1 ec2-user ec2-user 43157 Aug 5 09:17 sample_1.jpg // -rw-rw-r-- 1 ec2-user ec2-user 24496 Aug 5 09:17 sample_2.jpg // ...
- Specify thumbnail width, quality, and format.
const {writeThumbnails} = require('pdf2thumbnail'); writeThumbnails('sample.pdf', './dir', { width: 300, quality: 100, format: 'png' }); // $ ll dir // -rw-rw-r-- 1 ec2-user ec2-user 42722 Aug 5 09:17 sample_1.png // -rw-rw-r-- 1 ec2-user ec2-user 22411 Aug 5 09:17 sample_2.png // ...
- Thumbnail of the first page only.
const {writeThumbnails} = require('pdf2thumbnail'); writeThumbnails('sample.pdf', './dir', { start: 1, end: 1 }); // $ ll dir // -rw-rw-r-- 1 ec2-user ec2-user 43157 Aug 5 09:17 sample_1.jpg
- Thumbnails for pages 2-5.
const {writeThumbnails} = require('pdf2thumbnail'); writeThumbnails('sample.pdf', './dir', { start: 2, end: 5 }); // $ ll dir // -rw-rw-r-- 1 ec2-user ec2-user 24496 Aug 5 09:17 sample_2.jpg // -rw-rw-r-- 1 ec2-user ec2-user 41054 Aug 5 09:17 sample_3.jpg // -rw-rw-r-- 1 ec2-user ec2-user 45361 Aug 5 09:17 sample_4.jpg // -rw-rw-r-- 1 ec2-user ec2-user 38269 Aug 5 09:17 sample_5.jpg
-
Get the total number of pages in the PDF document.
Parameters:
filePath: string
: Path of the PDF file.
Return value:
{Promise<number>}
: Total number of pages in the PDF document.
-
Write a thumbnail for each page of the PDF document.
Parameters:
filePath: string
: Path of the PDF file.outDir: string
: Directory path to output thumbnails.options.width?: number
: Width of output thumbnail (px). Default is 300 (px).options.quality?: number
: The quality of the thumbnail to output (1-100). Default is 100.options.format?: string
: The format of the output thumbnail. Default is jpg.options.start?: number
: Start page position.options.end?: number
: End page position.
Takuya Motoshima