Skip to content
huzpsb edited this page Oct 26, 2026 · 1 revision

Render

render rasterizes PDF pages to image files — PNG or JPEG, at any DPI, from the same single binary. It powers PDF→thumbnail, PDF→image and PDF→OCR pipelines without poppler, mupdf or ImageMagick.

pdfcpu2 render in.pdf ./pages                # all pages, PNG @ 150 dpi
pdfcpu2 render -m jpg -q 90 --dpi 300 in.pdf ./pages
pdfcpu2 render -p 1-10 in.pdf ./pages
cat in.pdf | pdfcpu2 render - ./pages        # stdin

Command line

pdfcpu2 render [-p pageSelection] [--dpi n] [-m png|jpg] [--quality n] inFile outDir
Arg Meaning
inFile input PDF, or - for stdin
outDir output directory (created if missing; refuses to overwrite non-empty dirs unless --force)
-p, --pages page selection, see Page Selection
--dpi resolution in dots per inch (default 150)
-m, --mode png (default, lossless) or jpg
--quality JPEG quality 1–100 (default 85; PNG ignores it)
--opw / --upw passwords for encrypted input

Output files are named <inFile>_page_<pageNr>.<ext>, e.g. report_page_1.png.

Go API

package main

import (
	"context"
	"fmt"
	"os"

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

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

	o := &render.Options{DPI: 150, Format: "png"}

	// One file per page into outDir (same naming as the CLI).
	err := api.RenderPagesFile(ctx, "in.pdf", "pages", nil, o, conf)
	if err != nil {
		panic(err)
	}

	// Or digest results yourself — image.RGBA, page size in points, warnings.
	f, _ := os.Open("in.pdf")
	defer f.Close()
	err = api.RenderPages(ctx, f, []string{"1-3"}, o, func(res *render.Result) error {
		fmt.Println(res.PageNr, res.Image.Bounds(), res.Warnings)
		return nil
	}, conf)
	if err != nil {
		panic(err)
	}
}

Low level: render.Page(ctx, modelCtx, pageNr, o) renders one page of an already-read model.Context and returns a *render.Result (.Image *image.RGBA, .WidthPt/.HeightPt, .Warnings).

What the rasterizer covers

Self-contained pure-Go engine, differential-tested against PDFBox on real-world documents:

  • Graphics — anti-aliased fills/strokes/clips (4× supersampling), dashes, miter/bevel joins, transparency groups, soft masks (/SMask), blend alpha (ca/CA).
  • Text — embedded TrueType/OpenType, CID-keyed fonts, bare Type1C/CFF charstrings; ToUnicode, embedded cmaps and legacy GBK/Big5/Shift-JIS/EUC-KR encodings. Pages rotated via /Rotate come out upright.
  • Images — DCT (JPEG, incl. Adobe CMYK), Flate, CCITT G3/G4, with explicit masks and soft masks.
  • Color — DeviceGray/RGB/CMYK, CalGray/CalRGB, Lab, ICC-based, Indexed, Separation with tint functions, axial/radial shadings with interpolated sampling.
  • Fallback font — text without an embedded font program (including the PDF core fonts) is drawn with the bundled vivo Sans (see Home license notes).

Limitations

  • JPX (JPEG 2000) and JBIG2 images are not decoded yet — they render as a gray placeholder and produce a warning.
  • Pattern fills (scn with shading patterns) are skipped.
  • Per-page pixel count is capped by the resource limits (MaxImagePixels); lower --dpi for very large pages.

See also

  • Solidify — bake the rendering back into the PDF as image-only pages.
  • OCR — add an invisible text layer to scans.

Clone this wiki locally