Skip to content

Instantly share code, notes, and snippets.

@yinheli
Last active April 7, 2026 20:01
Show Gist options
  • Select an option

  • Save yinheli/069b2cda006347c9902a33bbf2378c2f to your computer and use it in GitHub Desktop.

Select an option

Save yinheli/069b2cda006347c9902a33bbf2378c2f to your computer and use it in GitHub Desktop.
cpdf Docker Quick Reference - Build & use cpdf (Coherent PDF) via Alpine Docker image
FROM alpine:edge
RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
&& apk update \
&& apk add --no-cache cpdf
ENTRYPOINT ["cpdf"]

cpdf Docker Quick Reference

Docker Hub

Pre-built image available at yinheli/cpdf.

Note: The image is currently arm64 only (built on Apple Silicon macOS).

docker pull yinheli/cpdf

Build Image (from source)

docker build -t cpdf .

Common Usage Pattern

# Using Docker Hub image
docker run --rm -v "$(pwd):/work" yinheli/cpdf [options] "/work/input.pdf" [page-range] -o "/work/output.pdf"

# Using locally built image
docker run --rm -v "$(pwd):/work" cpdf [options] "/work/input.pdf" [page-range] -o "/work/output.pdf"

Page Range Syntax

Range Description
1-10 Pages 1 to 10
1,3,5 Specific pages
1-6,9-end Pages 1-6 and 9 to last page
even Even pages only
odd Odd pages only
reverse All pages in reverse order
1-10,20-end Multiple ranges combined

File Information

# Get page count
cpdf -pages input.pdf

# Get file info (title, author, page size, etc.)
cpdf -info input.pdf

# Get per-page info
cpdf -page-info input.pdf

Split & Merge

# Extract page range (bookmarks auto-preserved)
cpdf input.pdf 1-100 -o part1.pdf

# Split into individual pages (one file per page)
cpdf -split input.pdf -o page%%%.pdf

# Split into chunks of N pages
cpdf -split input.pdf -chunk 50 -o chunk%%%.pdf

# Split at bookmark level (e.g., level 0 = top-level chapters)
cpdf -split-bookmarks 0 input.pdf -o chapter%%%.pdf

# Merge multiple files into one
cpdf -merge file1.pdf file2.pdf file3.pdf -o merged.pdf

# Merge with bookmarks for each file
cpdf -merge -merge-add-bookmarks file1.pdf file2.pdf -o merged.pdf

Bookmarks (Table of Contents)

# List bookmarks
cpdf -list-bookmarks input.pdf

# List bookmarks in JSON format
cpdf -list-bookmarks-json input.pdf

# Export bookmarks to file
cpdf -list-bookmarks input.pdf > bookmarks.txt

# Remove all bookmarks
cpdf -remove-bookmarks input.pdf -o output.pdf

# Add bookmarks from file
cpdf -add-bookmarks bookmarks.txt input.pdf -o output.pdf

# Open bookmarks to a specific level (0 = all closed)
cpdf -bookmarks-open-to-level 2 input.pdf -o output.pdf

Rotate & Transform

# Set rotation (0, 90, 180, 270)
cpdf -rotate 90 input.pdf -o output.pdf

# Rotate specific pages
cpdf -rotate 90 input.pdf 1-5 -o output.pdf

# Flip horizontally / vertically
cpdf -hflip input.pdf -o output.pdf
cpdf -vflip input.pdf -o output.pdf

# Scale page
cpdf -scale-page "2.0 2.0" input.pdf -o output.pdf

# Scale to fit a specific page size (in points, A4 = 595 842)
cpdf -scale-to-fit "595 842" input.pdf -o output.pdf

Page Layout

# 2-up (two pages on one)
cpdf -twoup input.pdf -o output.pdf

# Impose N-up onto given page size
cpdf -impose "595 842" input.pdf -o output.pdf

# Add blank page after every N pages
cpdf -pad-every 2 input.pdf -o output.pdf

# Pad to multiple of N pages
cpdf -pad-multiple 4 input.pdf -o output.pdf

Watermark & Stamp

# Stamp a PDF on top of pages
cpdf -stamp-on stamp.pdf input.pdf -o output.pdf

# Stamp underneath
cpdf -stamp-under stamp.pdf input.pdf -o output.pdf

# Add text watermark (all pages)
cpdf -add-text "DRAFT" -font "Helvetica" -font-size 48 -diagonal input.pdf -o output.pdf

# Add text to specific pages
cpdf -add-text "Page %Page" -bottomright 50 input.pdf 1-10 -o output.pdf

Crop

# Crop pages (x y w h in points)
cpdf -crop "50 50 500 750" input.pdf -o output.pdf

# Remove crop box
cpdf -remove-crop input.pdf -o output.pdf

Encryption

# Encrypt with password (128-bit AES)
cpdf -encrypt AES128 "owner_pass" "user_pass" input.pdf -o encrypted.pdf

# Decrypt
cpdf -decrypt encrypted.pdf -pw=owner_pass -o decrypted.pdf

Metadata

# Set title / author
cpdf -set-title "My Document" input.pdf -o output.pdf
cpdf -set-author "Author Name" input.pdf -o output.pdf

# List fonts used
cpdf -list-fonts input.pdf

# List annotations
cpdf -list-annotations input.pdf

Compression

# Compress streams
cpdf -compress input.pdf -o output.pdf

# Decompress (useful for debugging)
cpdf -decompress input.pdf -o output.pdf

Practical Examples

Split PDF into 2 equal parts

# Step 1: Get page count
PAGES=$(docker run --rm -v "$(pwd):/work" cpdf -pages "/work/input.pdf")

# Step 2: Calculate midpoint
MID=$(( (PAGES + 1) / 2 ))

# Step 3: Split
docker run --rm -v "$(pwd):/work" cpdf "/work/input.pdf" 1-$MID -o "/work/input_1.pdf"
docker run --rm -v "$(pwd):/work" cpdf "/work/input.pdf" $((MID+1))-$PAGES -o "/work/input_2.pdf"

Split PDF into N equal parts

#!/bin/sh
FILE="input.pdf"
N=3
PAGES=$(docker run --rm -v "$(pwd):/work" cpdf -pages "/work/$FILE")
CHUNK=$(( (PAGES + N - 1) / N ))

for i in $(seq 1 $N); do
  START=$(( (i - 1) * CHUNK + 1 ))
  END=$(( i * CHUNK ))
  [ $END -gt $PAGES ] && END=$PAGES
  docker run --rm -v "$(pwd):/work" cpdf "/work/$FILE" $START-$END -o "/work/${FILE%.pdf}_$i.pdf"
done

Extract and re-add bookmarks

# Export bookmarks
docker run --rm -v "$(pwd):/work" cpdf -list-bookmarks "/work/input.pdf" > bookmarks.txt

# Edit bookmarks.txt as needed, then re-apply
docker run --rm -v "$(pwd):/work" cpdf -add-bookmarks "/work/bookmarks.txt" "/work/input.pdf" -o "/work/output.pdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment