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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@chriscdn/pdf-thumbnails

v0.0.6

Published

PDF Thumbnail Generation

Readme

@chriscdn/pdf-thumbnails

This package generates and caches thumbnail images of a PDF file.

Installation

Using npm:

npm install @chriscdn/pdf-thumbnails

Using yarn:

yarn add @chriscdn/pdf-thumbnails

Dependencies

This package relies on two external utilities for operation:

  • pdftoppm from poppler-utils
  • convert from ImageMagick.

The process first converts a PDF page to PPM with pdftoppm before convering it to JPG with convert. After testing several methods, this approach seems to be the most efficient for generating high-quality thumbnails.

The package extends @chriscdn/file-cache to manage the cache of JPGs.

Usage

import { PDFThumbnailFileCache } from "@chriscdn/pdf-thumbnails";

// not required, but helps with duration calculations
import { Duration } from "@chriscdn/duration";

const options: PDFThumbnailFileCacheOptions = {
  // The path to the cache directory. Do not store anything else in this directory.
  cachePath: "/tmp/thumbnails/",

  // Optional, automatically create the cachePath if it doesn't exist.
  autoCreateCachePath: false,

  // Determines the time-to-live (TTL) of a cached file, in milliseconds,
  // based on when it was last accessed.
  ttl: Duration.toMilliseconds({ days: 7 }),

  // How often the cleanup task should run to purge expired cached files, in milliseconds.
  cleanupInterval: Duration.toMilliseconds({ hours: 4 }),

  // Optional, the image quality used by `convert`, which defaults to 70.
  quality: 70,

  // Optional, the path to pdftoppm. If omitted, it will look for `pdftoppm` in the system path.
  pdftoppm: "/usr/bin/pdftoppm",

  // Optional, the path to convert. If omitted, it will look for `convert` in the system path.
  convert: "/usr/bin/convert",
};

const thumbnailCache = new PDFThumbnailFileCache(options);

A thumbnail can be generated with:

const thumbnailFilePath = await thumbnailCache.getFile({
  pdfFilePath: "/some/pdf/file.pdf",
  pageIndex: 0, // 0-based
});

// This returns a file path to the `JPG` image. E.g.,
// /tmp/thumbnails/d/b/c/dbc7181baf54aa79b182142f12a8bee77068c74b.jpg

NOTE: The JPG file paths are determined by hashing the input parameters to getFile. To avoid caching conflicts, it's important to use a unique file path for different PDF files.

A pageCount method exists to retrieve the number of pages in a PDF:

const pageCount: number = await thumbnailCache.pageCount("/some/pdf/file.pdf");

Cleanup

PDFThumbnailFileCache instances are intended to be instantiated as singletons, persisting throughout the lifecycle of your app. If you need to deallocate an instance, be sure to call the destroy() method to prevent a memory leak. This does not cleanup the cache directory.

Tests

Run the tests using:

yarn test

License

MIT