PDF tools to merge PDF files (using JSON5 or
YAML or TOML file) and
extract pages (from command line or using a text file).
This cli exposes two binaries: pdftools
and pdf-tools
.
It can be used to update PDF's metadata using the process
command,
see test/docs/example.yaml for more details.
-
Node.js (at least v18.11.0)
-
-
Offical:
- Windows & macOS: https://www.pdflabs.com/tools/pdftk-server/
- Windows (winget):
winget install PDFLabs.PDFtk.Server
-
pdftk-java:
- Ubuntu:
sudo apt install pdftk-java
- macOS & Linux (Homebrew):
brew install pdftk-java
- Windows (Chocolatey):
choco install pdftk-java --params "'/AddToUserPath:yes /AddToSystemPath:yes'"
- Ubuntu:
-
Note: You might be interested in a GUI like PDF Chain.
-
-
mutool for some commands:
- Ubuntu:
sudo apt install mupdf-tools
- macOS & Linux (Homebrew):
brew install mupdf
- Windows:
- winget:
winget install ArtifexSoftware.mutool
(maybe justmutool
) - Chocolatey:
choco install mupdf
- winget:
- Ubuntu:
Let me know if I should use any other program or library to perform the current tasks or any other suggested tasks.
$ npm install -g @bader-nasser/pdftools
$ pdftools COMMAND
running command...
$ pdftools (--version|-v)
@bader-nasser/pdftools/3.1.0 linux-x64 node-v21.1.0
$ pdftools --help [COMMAND]
USAGE
$ pdftools COMMAND
...
There are many commands but the most important ones are:
In the JSON data file you can add:
"$schema": "https://github.com/bader-nasser/pdftools/raw/main/data.schema.json",
to get some help in your editor.
pdftools autocomplete [SHELL]
pdftools compress INPUT
pdftools compress2
pdftools convert INPUT
pdftools extract
pdftools help [COMMANDS]
pdftools merge
pdftools merge2
pdftools plugins
pdftools plugins:inspect PLUGIN...
pdftools plugins:install PLUGIN...
pdftools plugins:link PLUGIN
pdftools plugins:uninstall PLUGIN...
pdftools plugins update
pdftools process FILE
pdftools repair INPUT
pdftools split INPUT
pdftools uncompress INPUT
pdftools update-metadata INPUT
Display autocomplete installation instructions.
USAGE
$ pdftools autocomplete [SHELL] [-r]
ARGUMENTS
SHELL (zsh|bash|powershell) Shell type
FLAGS
-r, --refresh-cache Refresh cache (ignores displaying instructions)
DESCRIPTION
Display autocomplete installation instructions.
EXAMPLES
$ pdftools autocomplete
$ pdftools autocomplete bash
$ pdftools autocomplete zsh
$ pdftools autocomplete powershell
$ pdftools autocomplete --refresh-cache
See code: @oclif/plugin-autocomplete
Restore page stream compression
USAGE
$ pdftools compress INPUT [-D] [-s] [-o <value>]
ARGUMENTS
INPUT Uncompressed PDF file
FLAGS
-D, --dry-run Pretend to work!
-o, --output=<value> Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Restore page stream compression
ALIASES
$ pdftools c
EXAMPLES
$ pdftools compress uncompressed.pdf
$ pdftools compress uncompressed.pdf -o compressed.pdf
See code: src/commands/compress/index.ts
Compress streams [mutool]
USAGE
$ pdftools compress2 -i <value> [-D] [-s] [-o <value>] [-l] [-p <value>] [-m]
FLAGS
-D, --dry-run Pretend to work!
-i, --input=<value> (required) Uncompressed PDF file
-l, --linearize linearize PDF (optimize for web browsers) (ALIASES: -O, --optimize)
-m, --metadata preserve metadata
-o, --output=<value> Output file
-p, --pages=<value>... comma/space separated list of page numbers and ranges
-s, --silent Work silently unless there is an error!
DESCRIPTION
Compress streams [mutool]
ALIASES
$ pdftools c2
EXAMPLES
$ pdftools compress2 -i uncompressed.pdf
$ pdftools compress2 -i uncompressed.pdf -o compressed.pdf
See code: src/commands/compress2/index.ts
Convert PDF to text file
USAGE
$ pdftools convert INPUT [-D] [-s] [-o <value>]
ARGUMENTS
INPUT PDF file to convert
FLAGS
-D, --dry-run Pretend to work!
-o, --output=<value> Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Convert PDF to text file
EXAMPLES
$ pdftools convert file.pdf
$ pdftools convert file.pdf -o file-text.txt
See code: src/commands/convert/index.ts
Extract pages from PDF file
USAGE
$ pdftools extract -i <value> -o <value> [-D] [-s] [-c] [-f <value> | -p <value> | -d <value>] [-l <value>
| | ] [-q even|odd] [-r north|south|east|west|left|right|down] [-k]
FLAGS
-D, --dry-run Pretend to work!
-c, --compress Reduce file size
See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-compress
You also may want to try: https://www.ilovepdf.com/compress_pdf
-d, --data=<value> Data file (lines of page ranges)
See: https://github.com/bader-nasser/pdftools/blob/main/test/docs/data.txt
-f, --first-page=<value> First page (defaults to last-page)
-i, --input=<value> (required) Relative or absolute path to the PDF file to be used.
Use / in the path. On Windows, \ can be changed to either / or \\.
Surround the path by " or ' if it contains spaces.
-k, --keep Keep output's name
-l, --last-page=<value> Last page (defaults to first-page)
-o, --output=<value> (required) Relative or absolute path to the PDF file to be created.
Use / in the path. On Windows, \ can be changed to either / or \\.
Surround the path by " or ' if it contains spaces.
-p, --page-ranges=<value> Comma/Space-seperated list of page ranges (eg. "1-3, 5east, 4, 7-10even, 22-11odd")
See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat
See also: https://github.com/bader-nasser/pdftools/blob/main/test/docs/data.txt
-q, --qualifier=<option> See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat
<options: even|odd>
-r, --rotation=<option> See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat
<options: north|south|east|west|left|right|down>
-s, --silent Work silently unless there is an error!
DESCRIPTION
Extract pages from PDF file
ALIASES
$ pdftools ext
$ pdftools ex
$ pdftools e
EXAMPLES
Extract page number 5 from input.pdf to output.pdf
$ pdftools extract --input input.pdf --output output.pdf -f 5
Extract page number 5 from input.pdf to output.pdf
$ pdftools extract -i input.pdf -o output.pdf -l 5
Extract pages from 1 to 3 from input.pdf to output.pdf
$ pdftools extract -i input.pdf -o output.pdf -f 1 -l 3
Extract *even* pages from 9 to 4, compress it and rotate it to the left
$ pdftools extract -i input.pdf -o output.pdf -f 9 -l 4 -c -r left -q even
Extract pages from 1 to 3, with the 5th page rotated to the east, and *odd* pages from 7 to 4
$ pdftools extract -i input.pdf -o output.pdf -p "1-3, 5east, 7-4odd"
Extract pages as declared in file.txt
$ pdftools extract -i input.pdf -o output.pdf --data file.txt
See code: src/commands/extract/index.ts
Display help for pdftools.
USAGE
$ pdftools help [COMMANDS] [-n]
ARGUMENTS
COMMANDS Command to show help for.
FLAGS
-n, --nested-commands Include all nested commands in the output.
DESCRIPTION
Display help for pdftools.
See code: @oclif/plugin-help
Merge PDFs
USAGE
$ pdftools merge -i <value> -o <value> [-D] [-s] [-c]
FLAGS
-D, --dry-run Pretend to work!
-c, --compress Reduce file size
See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-compress
You also may want to try: https://www.ilovepdf.com/compress_pdf
-i, --input=<value>... (required) Input files (e.g. cover.pdf part-*.pdf)
-o, --output=<value> (required) Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Merge PDFs
ALIASES
$ pdftools m
$ pdftools join
$ pdftools j
EXAMPLES
Merge all .pdf files
$ pdftools merge -i *.pdf -o output.pdf
Merge all .pdf files that start with input- & compress the output
$ pdftools merge -i input-*.pdf -o output.pdf -c
Merge cover.pdf with all .pdf files that start with input-, and notes.pdf
$ pdftools merge -i cover.pdf input-*.pdf notes.pdf -o output.pdf
See code: src/commands/merge/index.ts
Merge PDFs [mutool]
USAGE
$ pdftools merge2 -i <value> -o <value> [-D] [-s] [-c | -d] [-F | ] [-I | ] [-l] [-g] [-C] [-G] [-k]
FLAGS
-C, --garbage-compact ... and compact cross reference table (ALIASES: --gc, --compact)
-D, --dry-run Pretend to work!
-F, --compress-fonts Compress embedded fonts (ALIASES: --cf)
-G, --garbage-deduplicate ... and remove duplicate objects (ALIASES: --gd, --deduplicate)
-I, --compress-images Compress images (ALIASES: --ci)
-c, --compress Compress all streams
-d, --decompress Decompress all streams (except compress-fonts/images)
-g, --garbage Garbage collect unused objects
-i, --input=<value>... (required) PDF files followed by comma-seperated page numbers or ranges
(e.g. cover.pdf part-*.pdf file.pdf 2,11,4-6,10-8 otherfile.pdf)
-k, --keep Keep output's name
-l, --linearize Optimize for web browsers (ALIASES: -O, --optimize)
-o, --output=<value> (required) Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Merge PDFs [mutool]
ALIASES
$ pdftools m2
$ pdftools join2
$ pdftools j2
EXAMPLES
Merge all .pdf files
$ pdftools merge2 -i *.pdf -o output.pdf
Merge all .pdf files that start with input- & compress the output
$ pdftools merge2 -i input-*.pdf -o output.pdf -c
Merge cover.pdf with all .pdf files that start with input-, and notes.pdf
$ pdftools merge2 -i cover.pdf input-*.pdf notes.pdf -o output.pdf
Merge all .pdf files and optimize the output for web browsers
$ pdftools merge2 -i input-*.pdf -o output -l
See code: src/commands/merge2/index.ts
List installed plugins.
USAGE
$ pdftools plugins [--json] [--core]
FLAGS
--core Show core plugins.
GLOBAL FLAGS
--json Format output as json.
DESCRIPTION
List installed plugins.
EXAMPLES
$ pdftools plugins
See code: @oclif/plugin-plugins
Displays installation properties of a plugin.
USAGE
$ pdftools plugins:inspect PLUGIN...
ARGUMENTS
PLUGIN [default: .] Plugin to inspect.
FLAGS
-h, --help Show CLI help.
-v, --verbose
GLOBAL FLAGS
--json Format output as json.
DESCRIPTION
Displays installation properties of a plugin.
EXAMPLES
$ pdftools plugins:inspect myplugin
See code: @oclif/plugin-plugins
Installs a plugin into the CLI.
USAGE
$ pdftools plugins:install PLUGIN...
ARGUMENTS
PLUGIN Plugin to install.
FLAGS
-f, --force Run yarn install with force flag.
-h, --help Show CLI help.
-s, --silent Silences yarn output.
-v, --verbose Show verbose yarn output.
DESCRIPTION
Installs a plugin into the CLI.
Can be installed from npm or a git url.
Installation of a user-installed plugin will override a core plugin.
e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command
will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
the CLI without the need to patch and update the whole CLI.
ALIASES
$ pdftools plugins add
EXAMPLES
$ pdftools plugins:install myplugin
$ pdftools plugins:install https://github.com/someuser/someplugin
$ pdftools plugins:install someuser/someplugin
See code: @oclif/plugin-plugins
Links a plugin into the CLI for development.
USAGE
$ pdftools plugins:link PLUGIN
ARGUMENTS
PATH [default: .] path to plugin
FLAGS
-h, --help Show CLI help.
-v, --verbose
--[no-]install Install dependencies after linking the plugin.
DESCRIPTION
Links a plugin into the CLI for development.
Installation of a linked plugin will override a user-installed or core plugin.
e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
command will override the user-installed or core plugin implementation. This is useful for development work.
EXAMPLES
$ pdftools plugins:link myplugin
See code: @oclif/plugin-plugins
Removes a plugin from the CLI.
USAGE
$ pdftools plugins:uninstall PLUGIN...
ARGUMENTS
PLUGIN plugin to uninstall
FLAGS
-h, --help Show CLI help.
-v, --verbose
DESCRIPTION
Removes a plugin from the CLI.
ALIASES
$ pdftools plugins unlink
$ pdftools plugins remove
See code: @oclif/plugin-plugins
Update installed plugins.
USAGE
$ pdftools plugins update [-h] [-v]
FLAGS
-h, --help Show CLI help.
-v, --verbose
DESCRIPTION
Update installed plugins.
See code: @oclif/plugin-plugins
Merge PDF files using data file. Can be used to merge some pages from each file.
USAGE
$ pdftools process FILE [-D] [-s] [-c] [-k]
ARGUMENTS
FILE Data file to process (can be JSON5 or YAML or TOML)
See: https://github.com/bader-nasser/pdftools/blob/main/test/docs/data.json
See also: https://github.com/bader-nasser/pdftools/blob/main/test/docs/example.yaml
Set "$schema" to "https://github.com/bader-nasser/pdftools/raw/main/data.schema.json"
Use / in the paths. On Windows, \ can be changed to either / or \\
FLAGS
-D, --dry-run Pretend to work!
-c, --compress Reduce file size
See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-compress
You also may want to try: https://www.ilovepdf.com/compress_pdf
-k, --keep Keep output's name
-s, --silent Work silently unless there is an error!
DESCRIPTION
Merge PDF files using data file. Can be used to merge some pages from each file.
ALIASES
$ pdftools p
EXAMPLES
$ pdftools process data.json
See code: src/commands/process/index.ts
Repair a PDF's corrupted XREF table and stream lengths, if possible
USAGE
$ pdftools repair INPUT [-D] [-s] [-o <value>]
ARGUMENTS
INPUT Broken PDF
FLAGS
-D, --dry-run Pretend to work!
-o, --output=<value> Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Repair a PDF's corrupted XREF table and stream lengths, if possible
ALIASES
$ pdftools r
EXAMPLES
$ pdftools repair broken.pdf
$ pdftools repair broken.pdf -o fixed.pdf
See code: src/commands/repair/index.ts
Split each page into many tiles [mutool]
USAGE
$ pdftools split INPUT [-D] [-s] [-o <value>] [-x <value>] [-y <value>] [-r]
ARGUMENTS
INPUT Input PDF file
FLAGS
-D, --dry-run Pretend to work!
-o, --output=<value> Output file
-r, --r Split horizontally from right to left (default splits from left to right). (v1.23.0+)
-s, --silent Work silently unless there is an error!
-x, --x=<value> Pieces to horizontally divide each page into.
-y, --y=<value> Pieces to vertically divide each page into.
DESCRIPTION
Split each page into many tiles [mutool]
ALIASES
$ pdftools s
EXAMPLES
$ pdftools split input.pdf -x 2
$ pdftools split input.pdf -o splitted.pdf -y 2
$ pdftools split input.pdf -x 2 -y 3 -r
See code: src/commands/split/index.ts
Uncompress PDF page streams for editing the PDF in a text editor
USAGE
$ pdftools uncompress INPUT [-D] [-s] [-o <value>]
ARGUMENTS
INPUT Compressed PDF file
FLAGS
-D, --dry-run Pretend to work!
-o, --output=<value> Output file
-s, --silent Work silently unless there is an error!
DESCRIPTION
Uncompress PDF page streams for editing the PDF in a text editor
ALIASES
$ pdftools u
$ pdftools decompress
$ pdftools d
EXAMPLES
$ pdftools uncompress doc.pdf
$ pdftools uncompress doc.pdf -o doc-uncompressed.pdf
See code: src/commands/uncompress/index.ts
Update PDF's metadata
USAGE
$ pdftools update-metadata INPUT [-D] [-s] [-f <value>] [-t <value>] [-a <value>] [-S <value>] [-k <value>] [-p
<value>] [-c <value>] [-d <value>] [-m <value>] [-o <value>]
ARGUMENTS
INPUT Input PDF to update
FLAGS
-D, --dry-run Pretend to work!
-S, --subject=<value>
-a, --author=<value>
-c, --creator=<value>
-d, --creation-date=<value>
-f, --file=<value> Metadata file (.json or .yaml or .toml)
-k, --keywords=<value>...
-m, --modification-date=<value>
-o, --output=<value> Output file
-p, --producer=<value>
-s, --silent Work silently unless there is an error!
-t, --title=<value>
DESCRIPTION
Update PDF's metadata
ALIASES
$ pdftools up-meta
$ pdftools meta
EXAMPLES
$ pdftools update-metadata input.pdf -f meta.json
$ pdftools update-metadata input.pdf -f meta.toml -o updated.pdf
$ pdftools update-metadata input.pdf -o updated.pdf -f meta.yaml -a "Bader Nasser" -t awesome
See code: src/commands/update-metadata/index.ts
If you find the app useful, let others know about it :)