npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdf2thumbnail

v1.0.8

Published

Generate PDF thumbnails.

Downloads

116

Readme

pdf2thumbnail

Generate PDF thumbnails.
Click here to see the change log.
pdf2thumbnail.png

Requirements

  • imagemagick CLI >= v6.9.10
    Installation example:
    • For instance, if you're on OS X you can use Homebrew.
      brew install imagemagick
    • For Linux, use yum.
      sudo yum -y install ImageMagick

Supported OS

  • Linux
  • MAC

Installation

npm install --save pdf2thumbnail

API

  • pdf2thumbnail.getTotalNumberOfPages()

    Get the total number of pages in the PDF document.

    Syntax

    pdf2thumbnail.getTotalNumberOfPages(pdfPathOrDataUrl);

    Usage

    const pdf2thumbnail = require('pdf2thumbnail');
    
    const totalPages = await pdf2thumbnail.getTotalNumberOfPages('sample.pdf');

    Parameters

    • {string} pdfPathOrDataUrl The path to the PDF file, DataURL.

    Return value

    {Promise<number>} Total number of pages in the PDF document.

    Throws

    • {TypeError} PDF path is empty.
    • {TypeError} Cannot find file in PDF path.
  • pdf2thumbnail.writeThumbnails()

    Write a thumbnail for each page of the PDF document.

    Syntax

    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir);
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg'});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg', start: 1});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg', start: 1, end: 2});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg', start: 1, end: 2, archive: true});   
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg', start: 1, end: 2, archive: true, background: '#000'});
    pdf2thumbnail.writeThumbnails(pdfPathOrDataUrl, outputDir, {width: 595, quality: 100, format: 'jpg', start: 1, end: 2, archive: true, background: '#000', offset: 30});

    Usage

    • Thumbnail all pages.
      The thumbnail file name is "<original file name>_<page number>.<extension>".
      const pdf2thumbnail = require('pdf2thumbnail');
      
      await pdf2thumbnail.writeThumbnails('sample.pdf', './result');
      // $ ll result
      // -rw-rw-r-- 1 ec2-user ec2-user 101656 Dec 20 18:49 sample_1.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 141934 Dec 20 18:49 sample_2.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 128636 Dec 20 18:49 sample_3.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 384545 Dec 20 18:49 sample.jpg
    • Specify thumbnail width, quality, and format.
      const pdf2thumbnail = require('pdf2thumbnail');
      
      await pdf2thumbnail.writeThumbnails('sample.pdf', './result', {width: 595, quality: 100, format: 'png'});
      // $ ll result
      // -rw-rw-r-- 1 ec2-user ec2-user 106689 Dec 20 18:49 sample_1.png
      // -rw-rw-r-- 1 ec2-user ec2-user 169000 Dec 20 18:49 sample_2.png
      // -rw-rw-r-- 1 ec2-user ec2-user 132571 Dec 20 18:49 sample_3.png
      // -rw-rw-r-- 1 ec2-user ec2-user 361910 Dec 20 18:49 sample.png
    • Thumbnails for pages 2-3.
      const pdf2thumbnail = require('pdf2thumbnail');
      
      await pdf2thumbnail.writeThumbnails('sample.pdf', './result', {start: 2, end: 3});
      // $ ll result
      // -rw-rw-r-- 1 ec2-user ec2-user 141934 Dec 20 18:49 sample_2.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 128636 Dec 20 18:49 sample_3.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 283503 Dec 20 18:49 sample.jpg
    • An archive containing all thumbnails is also generated at the same time.
      const pdf2thumbnail = require('pdf2thumbnail');
      
      await pdf2thumbnail.writeThumbnails('sample.pdf', './result', {archive: true});
      // ll result*
      // -rw-rw-rw- 1 ec2-user ec2-user 718211 Dec 20 18:04 result.zip
      
      // result:
      // -rw-rw-r-- 1 ec2-user ec2-user 101656 Dec 20 18:04 sample_1.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 141934 Dec 20 18:04 sample_2.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 128636 Dec 20 18:04 sample_3.jpg
      // -rw-rw-r-- 1 ec2-user ec2-user 384545 Dec 20 18:04 sample.jpg

    Parameters

    • {string} pdfPathOrDataUrl The path to the PDF file, DataURL.
    • {string} outputDir Directory path to output thumbnails.
    • {number} options.width? Width of output thumbnail (px). Default is 595 (px).
    • {number} options.xDensity? Horizontal resolution. The unit is dpi and default is 288.
    • {number} options.yDensity? Vertical resolution. The unit is dpi and default is 288.
    • {number} options.quality? The quality of the thumbnail to output (1-100). Default is 100.
    • {string} options.format? The format of the output thumbnail. Default is jpg.
    • {number} options.start? Starting page position starting from 1.
    • {number} options.end? End page position starting from 1.
    • {boolean} options.archive?
      If true, it generates an archive (.zip) containing all images with the same name as the output directory (outputDir).
      Default is false.
    • {string|{r: number, g: number, b: number, alpha: number}} options.background?
      Background color of merged thumbnails.
      This option accepts a color name, a hex color, or a numerical RGB, RGBA, HSL, HSLA, CMYK, or CMYKA specification.
      For example, blue, #dddddff, rgb(255,255,255), etc.
      Default is white.
    • {number} options.offset? Offset (in pixels) between merged images. Default is 0.

    Return value

    {Promise<{thumbnailPaths: string[], mergedPath: string}>} Thumbnail Result.
    This is an object with the following elements.

    • {string[]} thumbnailPaths Path list of output thumbnail files.
      For example, sample_1.jpg, sample_2.jpg.
    • {string} mergedPath The path of the image file from which the thumbnails for each page are merged.
      For example, sample.jpg.
    • {string} archivePath? The path to the archive containing all thumbnails.
      This is only set if the archive option is true when creating thumbnails.

    Throws

    • {TypeError} PDF path is empty.
    • {TypeError} Cannot find file in PDF path.
    • {TypeError} The width option is not a number greater than 1.
    • {TypeError} The quality option is not a number between 1 and 100.
    • {TypeError} The start option is not a number greater than 1.
    • {TypeError} The end option is not a number greater than 1.
    • {TypeError} The offset option is not a number greater than 0.

Web Demo

Click here to learn how to use the web demo.

Testing

With npm do:

npm test

Author

Takuya Motoshima

License

MIT