feat(pdf): add the clear-metadata stage

This commit is contained in:
Julien Neuhart
2026-11-30 09:56:54 +01:00
parent b5b9c3c5a5
commit 2f725293c6
14 changed files with 128 additions and 11 deletions

View File

@@ -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"}
}

View File

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

View File

@@ -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"},

View File

@@ -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",

View File

@@ -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"},
} {

View File

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

View File

@@ -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) },

View File

@@ -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"},

View File

@@ -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 |

View File

@@ -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 |

View File

@@ -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):

View File

@@ -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 |

View File

@@ -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):

View File

@@ -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 |