Skip to content

chriscdn/pdf-split

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pdf-split

pdf-split is a package that splits a PDF into individual pages and caches the results on the file system. It relies on pdfcpu for the split operation and @chriscdn/file-cache for file caching and cleanup.

Installation

Using npm:

npm install @chriscdn/pdf-split

Using yarn:

yarn add @chriscdn/pdf-split

Usage

Create a PDFSplitFileCache instance:

import { PDFSplitFileCache, Rotate } from "@chriscdn/pdf-split";
import { Duration } from "@chriscdn/duration";

const splitCache = new PDFSplitFileCache({
  cachePath: "/path/to/cache/directory",
  ttl: Duration.toMilliseconds({ days: 7 }),
});

This assumes that pdfcpu is available on the system PATH. If it is not, you can provide the path to the binary by using the pdfcpu parameter in the constructor:

const splitCache = new PDFSplitFileCache({
  cachePath: "/path/to/cache/directory",
  ttl: Duration.toMilliseconds({ days: 7 }),
  pdfcpu: "/opt/homebrew/bin/pdfcpu",
});

The PDFSplitFileCache class extends FileCache from @chriscdn/file-cache. All constructor arguments from FileCache are supported except for cb and ext. The cache, including automatic cleanup of expired files, is managed by FileCache.

Retrieve the file path to a PDF page:

const firstPageFilePath = await splitCache.getFile({
  pdfFilePath: "/path/to/your/pdf/file.pdf",
  pageIndex: 0,
  rotate: Rotate.DEG_0,
});

Notes:

  • pageIndex is 0-based.
  • rotate is optional, and can be set to DEG_0 (default), DEG_90, DEG_180, or DEG_270
  • The cache key is based on pdfFilePath and pageIndex. Ensure that unique PDFs have unique names to avoid cache collisions.

Other methods

Get the page count:

const pageCount = await splitCache.pageCount("/path/to/your/pdf/file.pdf");

Return an array containing the full path to each page of the split PDF. The length of the array should match pageCount:

const pages = await splitCache.pages("/path/to/your/pdf/file.pdf");

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published