From 2f725293c6892eb2f3ec93c166996057c3cdda6a Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 30 Nov 2026 09:56:54 +0100 Subject: [PATCH] feat(pdf): add the clear-metadata stage --- .bruno/PDF/Edit/Clear Metadata.bru | 23 ++++++++++ pkg/modules/pdf/edits.go | 9 ++++ pkg/modules/pdf/executor_test.go | 1 + pkg/modules/pdf/routes.go | 2 +- pkg/modules/pdf/routes_test.go | 30 +++++++++++- pkg/modules/pdf/stage_types.go | 13 +++++- pkg/modules/pdf/stages.go | 1 + pkg/modules/pdf/stages_test.go | 2 +- .../features/chromium_convert_html.feature | 2 +- .../chromium_convert_markdown.feature | 2 +- .../features/chromium_convert_url.feature | 2 +- .../features/libreoffice_convert.feature | 2 +- .../integration/features/pdf_metadata.feature | 46 ++++++++++++++++++- test/integration/features/pdf_pipe.feature | 4 +- 14 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 .bruno/PDF/Edit/Clear Metadata.bru diff --git a/.bruno/PDF/Edit/Clear Metadata.bru b/.bruno/PDF/Edit/Clear Metadata.bru new file mode 100644 index 00000000..ca7c65df --- /dev/null +++ b/.bruno/PDF/Edit/Clear Metadata.bru @@ -0,0 +1,23 @@ +meta { + name: Clear Metadata + type: http + seq: 9 +} + +post { + url: {{baseUrl}}/forms/pdf/clear-metadata + body: multipartForm + auth: none +} + +body:multipart-form { + files: @file(../test/integration/testdata/titled_alpha.pdf) +} + +headers { + ~Gotenberg-Output-Filename: cleared + ~Gotenberg-Webhook-Url: http://localhost:8080/webhook + ~Gotenberg-Webhook-Events-Url: http://localhost:8080/webhook/events + ~Gotenberg-Webhook-Method: POST + ~Gotenberg-Webhook-Extra-Http-Headers: {"X-Custom":"value"} +} diff --git a/pkg/modules/pdf/edits.go b/pkg/modules/pdf/edits.go index 59e1d765..1b68e86a 100644 --- a/pkg/modules/pdf/edits.go +++ b/pkg/modules/pdf/edits.go @@ -177,6 +177,14 @@ func (s *setMetadataStage) edit(_ context.Context, w *litho.Writer, _ *runEnv) e return nil } +func (*clearMetadataStage) edit(_ context.Context, w *litho.Writer, _ *runEnv) error { + // The rewrite this forces leaves the old entries nowhere in the file, as + // an incremental update would. + w.ClearMetadata() + + return nil +} + func (s *setBookmarksStage) edit(_ context.Context, w *litho.Writer, env *runEnv) error { // litho writes a bookmark to a page the document lacks as a dead one. err := validateBookmarkPages(s.Bookmarks, "bookmarks", env.pageCount) @@ -557,6 +565,7 @@ var ( _ editor = (*stampImageStage)(nil) _ editor = (*stampPdfStage)(nil) _ editor = (*setMetadataStage)(nil) + _ editor = (*clearMetadataStage)(nil) _ editor = (*setBookmarksStage)(nil) _ editor = (*setFormStage)(nil) _ editor = (*flattenFormStage)(nil) diff --git a/pkg/modules/pdf/executor_test.go b/pkg/modules/pdf/executor_test.go index a6bc004f..83a7a0eb 100644 --- a/pkg/modules/pdf/executor_test.go +++ b/pkg/modules/pdf/executor_test.go @@ -1257,6 +1257,7 @@ func TestRunSignedInput(t *testing.T) { {name: "insert", stages: `[{"op":"insert","source":"page_2.pdf"}]`, fixtures: []string{"page_2.pdf"}, wantOp: "insert"}, {name: "encrypt", stages: `[{"op":"encrypt","userPassword":"u"}]`, wantOp: "encrypt"}, {name: "flatten-form", stages: `[{"op":"flatten-form"}]`, wantOp: "flatten-form"}, + {name: "clear-metadata", stages: `[{"op":"clear-metadata"}]`, wantOp: "clear-metadata"}, {name: "redact", stages: `[{"op":"redact","regions":[{"page":1,"x":0,"y":0,"width":612,"height":792}]}]`, wantOp: "redact"}, {name: "remediate", stages: `[{"op":"remediate","profile":"PDF/A-2b"}]`, wantOp: "remediate"}, {name: "convert-colors", stages: `[{"op":"convert-colors","target":"gray"}]`, wantOp: "convert-colors"}, diff --git a/pkg/modules/pdf/routes.go b/pkg/modules/pdf/routes.go index 45afbd26..6834fe51 100644 --- a/pkg/modules/pdf/routes.go +++ b/pkg/modules/pdf/routes.go @@ -514,7 +514,7 @@ var gotenberg8Routes = map[string]string{ "optimize": "/forms/pdf/optimize", "convert": "/forms/pdf/remediate", "metadata/read": "/forms/pdf/metadata", - "metadata/write": "/forms/pdf/set-metadata", + "metadata/write": "/forms/pdf/set-metadata, or /forms/pdf/clear-metadata to remove every field", "bookmarks/read": "/forms/pdf/bookmarks", "bookmarks/write": "/forms/pdf/set-bookmarks", "encrypt": "/forms/pdf/encrypt", diff --git a/pkg/modules/pdf/routes_test.go b/pkg/modules/pdf/routes_test.go index fb2bb55d..6a8bf969 100644 --- a/pkg/modules/pdf/routes_test.go +++ b/pkg/modules/pdf/routes_test.go @@ -505,6 +505,34 @@ func TestMetadataRoundTrip(t *testing.T) { } } +// clear-metadata removes the /Info entries and the XMP packet that would +// otherwise keep the same values. +func TestClearMetadata(t *testing.T) { + processor := testProcessor() + ctx, paths := testContext(t, "titled_alpha.pdf") + + input := openOutput(t, paths["titled_alpha.pdf"]) + xmp, err := input.XMPMetadata(context.Background()) + if err != nil || xmp == nil { + t.Fatalf("titled_alpha.pdf XMP: present %t, %v, want a packet", xmp != nil, err) + } + + outputs, err := processor.Run(ctx, mustStages(t, `[{"op":"clear-metadata"}]`), []string{paths["titled_alpha.pdf"]}, nil) + if err != nil { + t.Fatalf("clear-metadata: %v", err) + } + + got := readMetadata(t, processor, outputs[0]) + if got != (documentMetadata{}) { + t.Errorf("metadata = %+v, want none", got) + } + + xmp, err = openOutput(t, outputs[0]).XMPMetadata(context.Background()) + if err != nil || xmp != nil { + t.Errorf("XMP: present %t, %v, want none", xmp != nil, err) + } +} + // TestUnknownFields checks that the /forms/pdf routes answer 400 for a field // they do not read, and point a Gotenberg 8 one to its stage. func TestUnknownFields(t *testing.T) { @@ -619,7 +647,7 @@ func TestGotenberg8Route(t *testing.T) { wantMessage string }{ {path: "/forms/pdfengines/merge", wantMessage: "Gotenberg 9 replaced /forms/pdfengines/merge with /forms/pdf/merge"}, - {path: "/forms/pdfengines/metadata/write", wantMessage: "Gotenberg 9 replaced /forms/pdfengines/metadata/write with /forms/pdf/set-metadata"}, + {path: "/forms/pdfengines/metadata/write", wantMessage: "Gotenberg 9 replaced /forms/pdfengines/metadata/write with /forms/pdf/set-metadata, or /forms/pdf/clear-metadata to remove every field"}, {path: "/forms/pdfengines/watermark", wantMessage: "Gotenberg 9 replaced /forms/pdfengines/watermark with /forms/pdf/stamp-text, /forms/pdf/stamp-image or /forms/pdf/stamp-pdf"}, {path: "/forms/pdfengines/foo", wantMessage: "Gotenberg 9 replaced the /forms/pdfengines routes with the /forms/pdf routes"}, } { diff --git a/pkg/modules/pdf/stage_types.go b/pkg/modules/pdf/stage_types.go index f2dd93e7..f9fd5235 100644 --- a/pkg/modules/pdf/stage_types.go +++ b/pkg/modules/pdf/stage_types.go @@ -26,7 +26,7 @@ type signatureDrop struct { func (s signatureDrop) dropsSignatures() bool { return s.DropSignatures } // rewriter is a stage that rewrites a PDF whole, or builds a new one from it: -// merge, split, select, delete, insert, compose, flatten-form, +// merge, split, select, delete, insert, compose, clear-metadata, flatten-form, // flatten-transparency, convert-colors, redact, optimize, encrypt, decrypt and // remediate. The other edits append an incremental update, which keeps the // signatures. @@ -452,6 +452,16 @@ func (s *setMetadataStage) validate() error { func (*setMetadataStage) files() []string { return nil } +// clearMetadataStage removes the document information dictionary's entries +// and the catalog's XMP packet. +type clearMetadataStage struct { + signatureDrop +} + +func (*clearMetadataStage) op() string { return "clear-metadata" } +func (*clearMetadataStage) validate() error { return nil } +func (*clearMetadataStage) files() []string { return nil } + // setBookmarksStage replaces the outline. type setBookmarksStage struct { Bookmarks []models.Bookmark `json:"bookmarks"` @@ -1119,6 +1129,7 @@ var ( _ stage = (*stampPdfStage)(nil) _ stage = (*composeStage)(nil) _ stage = (*setMetadataStage)(nil) + _ stage = (*clearMetadataStage)(nil) _ stage = (*setBookmarksStage)(nil) _ stage = (*setFormStage)(nil) _ stage = (*flattenFormStage)(nil) diff --git a/pkg/modules/pdf/stages.go b/pkg/modules/pdf/stages.go index e98aae46..347256d2 100644 --- a/pkg/modules/pdf/stages.go +++ b/pkg/modules/pdf/stages.go @@ -48,6 +48,7 @@ var stageFactories = map[string]func() stage{ "stamp-pdf": func() stage { return new(stampPdfStage) }, "compose": func() stage { return new(composeStage) }, "set-metadata": func() stage { return new(setMetadataStage) }, + "clear-metadata": func() stage { return new(clearMetadataStage) }, "set-bookmarks": func() stage { return new(setBookmarksStage) }, "set-form": func() stage { return new(setFormStage) }, "flatten-form": func() stage { return new(flattenFormStage) }, diff --git a/pkg/modules/pdf/stages_test.go b/pkg/modules/pdf/stages_test.go index 4f051e27..bc32778f 100644 --- a/pkg/modules/pdf/stages_test.go +++ b/pkg/modules/pdf/stages_test.go @@ -52,7 +52,7 @@ func TestParseStages(t *testing.T) { {name: "nested bookmark without title", raw: `[{"op":"set-bookmarks","bookmarks":[{"title":"A","pageNumber":1,"children":[{"title":" ","pageNumber":1}]}]}]`, wantErr: `set-bookmarks: bookmarks[0].children[0].title is required`}, {name: "unknown permission", raw: `[{"op":"encrypt","userPassword":"u","permissions":{"printt":false}}]`, wantErr: `encrypt: "permissions" has an unknown key "printt"; the keys are print, printHighQuality, modifyContent, extractText, modifyAnnotations, fillForms and assemble`}, {name: "permissions", raw: `[{"op":"encrypt","userPassword":"u","permissions":{"print":false,"assemble":true}}]`, wantOps: []string{"encrypt"}}, - {name: "unknown op lists the operations", raw: `[{"op":"explode"}]`, wantErr: `unknown operation "explode"; the operations are attach, autotag, compose, convert-colors, decrypt, delete,`}, + {name: "unknown op lists the operations", raw: `[{"op":"explode"}]`, wantErr: `unknown operation "explode"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete,`}, {name: "missing pages", raw: `[{"op":"select"}]`, wantErr: `"pages" must not be empty`}, {name: "split needs one mode", raw: `[{"op":"split"}]`, wantErr: `set "every" or "ranges"`}, {name: "split modes exclusive", raw: `[{"op":"split","every":2,"ranges":["1"]}]`, wantErr: "not both"}, diff --git a/test/integration/features/chromium_convert_html.feature b/test/integration/features/chromium_convert_html.feature index 87db2458..c13a2cfc 100644 --- a/test/integration/features/chromium_convert_html.feature +++ b/test/integration/features/chromium_convert_html.feature @@ -1358,7 +1358,7 @@ Feature: /forms/chromium/convert/html Then the response header "Content-Type" should be "text/plain; charset=UTF-8" Then the response body should match string: """ - Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp + Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp """ When I make a "POST" request to Gotenberg at the "/forms/chromium/convert/html" endpoint with the following form data and header(s): | files | testdata/page-1-html/index.html | file | diff --git a/test/integration/features/chromium_convert_markdown.feature b/test/integration/features/chromium_convert_markdown.feature index b55edbfa..3b34b3d9 100644 --- a/test/integration/features/chromium_convert_markdown.feature +++ b/test/integration/features/chromium_convert_markdown.feature @@ -299,7 +299,7 @@ Feature: /forms/chromium/convert/markdown Then the response header "Content-Type" should be "text/plain; charset=UTF-8" Then the response body should match string: """ - Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp + Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp """ When I make a "POST" request to Gotenberg at the "/forms/chromium/convert/markdown" endpoint with the following form data and header(s): | files | testdata/page-1-markdown/index.html | file | diff --git a/test/integration/features/chromium_convert_url.feature b/test/integration/features/chromium_convert_url.feature index e4bf6117..c04c4751 100644 --- a/test/integration/features/chromium_convert_url.feature +++ b/test/integration/features/chromium_convert_url.feature @@ -833,7 +833,7 @@ Feature: /forms/chromium/convert/url Then the response header "Content-Type" should be "text/plain; charset=UTF-8" Then the response body should match string: """ - Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp + Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp """ Given I have a static server When I make a "POST" request to Gotenberg at the "/forms/chromium/convert/url" endpoint with the following form data and header(s): diff --git a/test/integration/features/libreoffice_convert.feature b/test/integration/features/libreoffice_convert.feature index aaf561b3..015a8810 100644 --- a/test/integration/features/libreoffice_convert.feature +++ b/test/integration/features/libreoffice_convert.feature @@ -545,7 +545,7 @@ Feature: /forms/libreoffice/convert Then the response header "Content-Type" should be "text/plain; charset=UTF-8" Then the response body should match string: """ - Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "foo"; the operations are attach, autotag, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp + Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "foo"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp """ When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s): | files | testdata/pages_3.docx | file | diff --git a/test/integration/features/pdf_metadata.feature b/test/integration/features/pdf_metadata.feature index 07d190d5..0e73faa6 100644 --- a/test/integration/features/pdf_metadata.feature +++ b/test/integration/features/pdf_metadata.feature @@ -1,6 +1,6 @@ @pdf @pdf-metadata -Feature: /forms/pdf/{set-metadata|metadata} +Feature: /forms/pdf/{set-metadata|clear-metadata|metadata} Scenario: POST /forms/pdf/{set-metadata|metadata} (Single PDF) Given I have a default Gotenberg container @@ -77,6 +77,50 @@ Feature: /forms/pdf/{set-metadata|metadata} Then the response status code should be 200 Then the "foo.pdf" PDF should have the "Title" metadata set to "Both" + # titled_alpha.pdf carries an /Info dictionary and an XMP packet: neither + # outlives the stage. + Scenario: POST /forms/pdf/clear-metadata + Given I have a default Gotenberg container + When I make a "POST" request to Gotenberg at the "/forms/pdf/clear-metadata" endpoint with the following form data and header(s): + | files | testdata/titled_alpha.pdf | file | + | Gotenberg-Output-Filename | foo | header | + Then the response status code should be 200 + Then the response header "Content-Type" should be "application/pdf" + Then there should be 1 PDF(s) in the response + When I make a "POST" request to Gotenberg at the "/forms/pdf/metadata" endpoint with the following form data and header(s): + | files | teststore/foo.pdf | file | + Then the response status code should be 200 + Then the response body should contain string: + """ + "foo.pdf":{} + """ + + Scenario: POST /forms/pdf/pipe (Clear, Then Set Metadata) + Given I have a default Gotenberg container + When I make a "POST" request to Gotenberg at the "/forms/pdf/pipe" endpoint with the following form data and header(s): + | files | testdata/titled_alpha.pdf | file | + | stages | [{"op":"clear-metadata"},{"op":"set-metadata","title":"Clean"}] | field | + | Gotenberg-Output-Filename | foo | header | + Then the response status code should be 200 + When I make a "POST" request to Gotenberg at the "/forms/pdf/metadata" endpoint with the following form data and header(s): + | files | teststore/foo.pdf | file | + Then the response status code should be 200 + Then the response body should contain string: + """ + "foo.pdf":{"title":"Clean"} + """ + + Scenario: POST /forms/pdf/clear-metadata (Bad Request) + Given I have a default Gotenberg container + When I make a "POST" request to Gotenberg at the "/forms/pdf/pipe" endpoint with the following form data and header(s): + | files | testdata/page_1.pdf | file | + | stages | [{"op":"clear-metadata","author":"Somebody"}] | field | + Then the response status code should be 400 + Then the response body should match string: + """ + Invalid form data: form field 'stages' is invalid: stage 0: clear-metadata: unknown option "author" + """ + Scenario: POST /forms/pdf/set-metadata (Bad Request) Given I have a default Gotenberg container When I make a "POST" request to Gotenberg at the "/forms/pdf/pipe" endpoint with the following form data and header(s): diff --git a/test/integration/features/pdf_pipe.feature b/test/integration/features/pdf_pipe.feature index b7ede763..42cf8f7d 100644 --- a/test/integration/features/pdf_pipe.feature +++ b/test/integration/features/pdf_pipe.feature @@ -88,7 +88,7 @@ Feature: /forms/pdf/pipe Then the response status code should be 400 Then the response body should match string: """ - Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp + Invalid form data: form field 'stages' is invalid: stage 0: unknown operation "shred"; the operations are attach, autotag, clear-metadata, compose, convert-colors, decrypt, delete, detach, encrypt, extract-attachments, extract-text, flatten-form, flatten-transparency, insert, merge, optimize, redact, remediate, render, rotate, select, set-bookmarks, set-form, set-metadata, sign, split, stamp-image, stamp-pdf, stamp-text, timestamp """ When I make a "POST" request to Gotenberg at the "/forms/pdf/pipe" endpoint with the following form data and header(s): | files | testdata/page_1.pdf | file | @@ -332,7 +332,7 @@ Feature: /forms/pdf/pipe Then the response status code should be 404 Then the response body should match string: """ - Gotenberg 9 replaced /forms/pdfengines/metadata/write with /forms/pdf/set-metadata + Gotenberg 9 replaced /forms/pdfengines/metadata/write with /forms/pdf/set-metadata, or /forms/pdf/clear-metadata to remove every field """ When I make a "POST" request to Gotenberg at the "/forms/pdfengines/foo" endpoint with the following form data and header(s): | files | testdata/page_1.pdf | file |