Skip to content

Quick Tour

huzpsb edited this page Oct 25, 2026 · 1 revision

Quick Tour

Ten minutes, ten commands — the fastest way to get a feel for pdfcpu2. Every command also has a Go API counterpart; see Go API.

1. Validate a PDF

pdfcpu2 validate in.pdf

Checks spec compliance (PDF 1.7 / basic PDF 2.0). Use -m strict for pedantic mode, -l to check for broken links.

2. Inspect a PDF

pdfcpu2 info in.pdf            # human readable
pdfcpu2 info --json in.pdf     # machine readable, great with jq

3. OCR a scan, then extract its text

The fork's headline act — fully offline, one binary:

pdfcpu2 ocr scan.pdf scan_ocr.pdf      # embed invisible text layer
pdfcpu2 extract -m text scan_ocr.pdf - # dump text to stdout

4. Split and merge

pdfcpu2 split in.pdf ./pages 1     # one PDF per page
pdfcpu2 merge out.pdf a.pdf b.pdf  # concatenate

5. Keep selected pages

pdfcpu2 trim -p 1-3,7 in.pdf out.pdf
pdfcpu2 collect -p 3,1,1 in.pdf out.pdf   # custom sequence, repeats allowed

6. Stamp pages

pdfcpu2 stamp add "CONFIDENTIAL" "pos:tr, scale:.35 abs, op:.6" in.pdf out.pdf

7. Watermark

pdfcpu2 watermark add "DRAFT" "diag:1, scale:.8 rel, op:.25" in.pdf out.pdf

8. 4-up handout

pdfcpu2 nup out.pdf 4 in.pdf

9. Encrypt

pdfcpu2 encrypt --opw owner --upw user in.pdf out.pdf   # AES-256

10. Text extraction from a digital PDF

pdfcpu2 extract -m text in.pdf out.txt          # whole document
pdfcpu2 extract -m text -p 1-3 in.pdf -         # pages 1-3 to stdout

The same tour in Go

package main

import (
	"context"

	"github.com/huzpsb/pdfcpu2/pkg/api"
	"github.com/huzpsb/pdfcpu2/pkg/pdfcpu/model"
)

func main() {
	ctx := context.Background()
	conf := model.NewDefaultConfiguration()

	_ = api.ValidateFile(ctx, "in.pdf", conf, nil)
	_, _ = api.OCRFile(ctx, "scan.pdf", "scan_ocr.pdf", nil, conf)
	_ = api.ExtractTextFile(ctx, "scan_ocr.pdf", "out.txt", nil, nil, conf)
	_ = api.MergeCreateFile(ctx, []string{"a.pdf", "b.pdf"}, "out.pdf", false, conf)
	_ = api.TrimFile(ctx, "in.pdf", "out.pdf", []string{"1-3", "7"}, conf)
}

Where to go next

Clone this wiki locally