Pdfsplit
pdfsplit is a Ruby CLI tool that splits a multi-page PDF into smaller PDF files with a fixed number of pages per part.
- Cross-platform: Linux / macOS / Windows
- No external PDF utilities required (no
pdftk,qpdf, etc.) - Uses the HexaPDF gem to handle PDF files
Features
- Splits
input.pdfinto files likeinput_part001.pdf,input_part002.pdf, ... - Part size is configured via
--pages N - Output directory is configured via
--out DIR(defaults to current directory.) -
--help,--version - Clear error messages and proper exit codes (0 = success, 1 = error)
Requirements
- Ruby 3.1+ (recommended)
Bundler is only needed for development / running from the repository.
After installing the gem, you can run thepdfsplitcommand directly.
Installation
Option A: Install from RubyGems (recommended)
gem install pdfsplitAfter installation the executable becomes available:
pdfsplit --helpOn Windows, if the
pdfsplitcommand is not found, ensure RubyGems' bin directory is in yourPATH(RubyInstaller usually sets this up automatically).
Option B: Install from the repository (for development)
Clone the repository and install dependencies:
bundle installUsage
Split a PDF into parts of N pages
Installed gem (recommended):
pdfsplit input.pdf --pages 10From repository:
bundle exec pdfsplit input.pdf --pages 10Outputs will be created in the current directory:
input_part001.pdfinput_part002.pdf- ...
Specify an output directory
pdfsplit INPUT.pdf --pages 10 --out outFiles will be created in out/.
Help
pdfsplit --helpVersion
pdfsplit --versionOutput naming
Given an input file input.pdf, the tool writes:
input_part001.pdfinput_part002.pdfinput_part003.pdf- ...
Errors & behavior
- Missing input file → exit 1 + Usage
- Missing
--pages/--pages <= 0→ exit 1 + Usage - More than one input file provided → exit 1 + Usage
- Input file does not exist → exit 1 + OS error message
- Input is not a valid PDF → exit 1 + Error: invalid PDF
-
--outpoints to an existing file → exit 1 + Error:--outmust be a directory - Output directory is not writable → exit 1 + OS error message
Development
Run tests:
bundle exec rspecInstall the gem locally (to test it as an installed gem):
bundle exec rake installOpen an interactive console (Bundler-generated helper):
bin/consoleLicense
MIT License