Skip to content

Attachments

huzpsb edited this page Oct 25, 2026 · 1 revision

Attachments

Manage embedded file attachments — the documents ride along inside the PDF.

Command line

pdfcpu2 attachments list in.pdf
pdfcpu2 attachments add in.pdf data.xlsx                       # add
pdfcpu2 attachments add in.pdf "song.mp3, audio" "film.mkv, video"   # with descriptions
pdfcpu2 attachments extract in.pdf outDir                      # extract all
pdfcpu2 attachments remove in.pdf                              # remove all
pdfcpu2 attachments remove in.pdf data.xlsx                    # remove specific

Pipeline:

cat report.pdf | pdfcpu2 attachments add - source.xlsx > report-with-source.pdf
cat report.pdf | pdfcpu2 attachments extract - ./attachments

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()

	f, _ := os.Open("in.pdf")
	defer f.Close()

	// Inventory
	aa, err := api.Attachments(ctx, f, conf)
	if err != nil {
		panic(err)
	}
	for _, a := range aa {
		fmt.Println(a.FileName, a.Desc)
	}

	// Add, remove, extract
	_ = api.AddAttachmentsFile(ctx, "in.pdf", "out.pdf", []string{"data.xlsx"}, false, conf)
	_ = api.RemoveAttachmentsFile(ctx, "in.pdf", "out2.pdf", nil, conf)
	_ = api.ExtractAttachmentsFile(ctx, "in.pdf", "./files", nil, conf)
}

The coll bool parameter routes files into a PDF collection instead of plain attachments — see Portfolio.

Clone this wiki locally