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

Create

Create page content — text, images, boxes, tables, forms — from a JSON description file. Either generate a new PDF or append content onto the pages of an existing one.

Command line

pdfcpu2 create inFileJSON [ inFile ] outFile
  • inFileJSON — the content description
  • inFile — optional existing PDF whose pages receive the content (- = stdin)
  • outFile — result (- = stdout); without inFile, outFile is created fresh

Minimal example (hello.json):

{
  "pages": {
    "1": {
      "content": {
        "text": [
          {
            "value": "Hello pdfcpu2 user!",
            "anchor": "center",
            "font": { "name": "Helvetica", "size": 12 }
          }
        ]
      }
    }
  }
}
pdfcpu2 create hello.json out.pdf
pdfcpu2 create invoice.json template.pdf invoice_out.pdf   # overlay onto template
cat template.pdf | pdfcpu2 create overlay.json - - > out.pdf

More examples live in the repository under pkg/testdata/json/ and pkg/samples/create/ — including boxes, tables, headers/footers, watermarked backgrounds and complete forms.

Content model (overview)

Each page entry ("1", "2", "1-3", "even", …) may contain:

  • text — positioned/anchored text runs with font, size, color, rotation, alignment
  • image — local or remote images with position, size, borders, links
  • boxes, tables — layout primitives
  • form fields (also see Form)

Local image paths are confined to an allowed directory — conf.LocalImageRoot for API callers; the CLI defaults to its working directory, localImageRoot in config.yml overrides (see Configuration).

Go API

package main

import (
	"context"
	"os"

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

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

	// New PDF from JSON
	err := api.CreateFile(ctx, "", "content.json", "out.pdf", conf)
	if err != nil {
		panic(err)
	}

	// Append content to an existing PDF
	err = api.CreateFile(ctx, "template.pdf", "overlay.json", "out.pdf", conf)
	_ = err

	// Streaming: JSON from an io.Reader, PDF written to an io.Writer
	jsonFile, _ := os.Open("content.json")
	defer jsonFile.Close()
	err = api.Create(ctx, nil, jsonFile, os.Stdout, conf)
	_ = err
}

Clone this wiki locally