fix(pdf): draw each set-form value into the field's widgets

This commit is contained in:
Julien Neuhart
2026-11-25 08:42:47 +01:00
parent 8b4a38bcf4
commit aaa1dd4853
11 changed files with 189 additions and 18 deletions

2
go.mod
View File

@@ -12,7 +12,7 @@ require (
github.com/gobwas/ws v1.5.0
github.com/gomarkdown/markdown v0.0.0-20261124143954-1814b856890d
github.com/google/uuid v1.6.0
github.com/gotenberg/litho v0.0.19
github.com/gotenberg/litho v0.0.20
github.com/hashicorp/go-retryablehttp v0.7.8
github.com/labstack/echo/v5 v5.4.7
github.com/labstack/gommon v0.5.0

4
go.sum
View File

@@ -122,8 +122,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/gotenberg/litho v0.0.19 h1:nu5OlKnGSjiC/Tzia682ZwM9oCf2NsXT8KY7TkA+a+I=
github.com/gotenberg/litho v0.0.19/go.mod h1:BYtOYpLnI02JzPt4FbeozPgTz1OrCPyyao5kAoEpn4Y=
github.com/gotenberg/litho v0.0.20 h1:rIAVZVpjDKbzbl0bxfAPe00f6jIpXK3GTJ2QJ96nJdM=
github.com/gotenberg/litho v0.0.20/go.mod h1:UHwIc1GVEcDdfIz004OeHOG6t9YD/otgO/gtCu5nD3s=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.34.0 h1:vzlTzzi4tHLa/JQdIE5LeXw6SEX5EVVhq/VPqKHnKOk=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.34.0/go.mod h1:FHXQZnW5pA0txiMr3jApv9uNJzZ+7mYZd+u+PPenCFU=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=

View File

@@ -292,3 +292,9 @@ func redactRetryLog(keysAndValues []any) []any {
var (
_ retryablehttp.LeveledLogger = (*LeveledLogger)(nil)
)
// LevelFilter returns a [slog.Handler] that drops the records of next below
// level.
func LevelFilter(next slog.Handler, level slog.Level) slog.Handler {
return log.LevelFilter(next, level)
}

View File

@@ -189,14 +189,17 @@ func (s *setBookmarksStage) edit(_ context.Context, w *litho.Writer, env *runEnv
return nil
}
func (s *setFormStage) edit(_ context.Context, w *litho.Writer, _ *runEnv) error {
func (s *setFormStage) edit(_ context.Context, w *litho.Writer, env *runEnv) error {
// litho draws each new value into its widgets; one outside Windows-1252
// takes a font installed in the image.
fonts := litho.WithFieldFonts(env.processor.textFonts)
for _, name := range sortedKeys(s.Fields) {
if selections, ok := s.selections[name]; ok {
w.SetFormFieldValues(name, selections)
w.SetFormFieldValues(name, selections, fonts)
continue
}
w.SetFormField(name, s.texts[name])
w.SetFormField(name, s.texts[name], fonts)
}
return nil

View File

@@ -214,8 +214,13 @@ func runEnd(stages Stages, start int) int {
// tree: litho opens a truncated or corrupt file lazily, and a route that never
// reads the pages, such as metadata, or a merge of one file, would answer a
// document it cannot read as if it were fine.
func (p *Processor) open(ctx context.Context, path, password string) (*litho.Reader, error) {
func (p *Processor) open(ctx context.Context, logger *slog.Logger, path, password string) (*litho.Reader, error) {
opts := []litho.OpenOption{litho.WithLimits(p.limits)}
if logger != nil {
// litho warns about what it could not do as asked, such as a form
// value no installed font draws; its debug records stay out.
opts = append(opts, litho.WithLogger(slog.New(gotenberg.LevelFilter(logger.Handler(), slog.LevelWarn))))
}
if password != "" {
opts = append(opts, litho.WithPassword(password))
}
@@ -313,7 +318,7 @@ func (p *Processor) merge(ctx *api.Context, s *mergeStage, docs []document, pass
// read all the same: merge must not hand back a file the other routes
// refuse.
if len(docs) == 1 && s.FileBookmarks == nil {
r, err := p.open(ctx, docs[0].path, password)
r, err := p.open(ctx, ctx.Log(), docs[0].path, password)
if err != nil {
return nil, &stageError{op: "merge", filename: docs[0].filename, err: err}
}
@@ -331,7 +336,7 @@ func (p *Processor) merge(ctx *api.Context, s *mergeStage, docs []document, pass
}()
for _, d := range docs {
r, err := p.open(ctx, d.path, password)
r, err := p.open(ctx, ctx.Log(), d.path, password)
if err != nil {
return nil, &stageError{op: "merge", filename: d.filename, err: err}
}
@@ -468,7 +473,7 @@ func (p *Processor) compose(ctx *api.Context, s *composeStage, docs []document,
}()
for _, d := range docs {
r, err := p.open(ctx, d.path, password)
r, err := p.open(ctx, ctx.Log(), d.path, password)
if err != nil {
return nil, &stageError{op: "compose", filename: d.filename, err: err}
}
@@ -559,7 +564,7 @@ func (p *Processor) split(ctx *api.Context, s *splitStage, docs []document, pass
}
func (p *Processor) splitOne(ctx *api.Context, s *splitStage, d document, password string) ([]document, error) {
r, err := p.open(ctx, d.path, password)
r, err := p.open(ctx, ctx.Log(), d.path, password)
if err != nil {
return nil, err
}
@@ -661,7 +666,7 @@ func (p *Processor) runEach(ctx *api.Context, run Stages, docs []document, passw
// runOne runs a run of stages on one document through a single litho
// pipeline. encryptionHandled tells that the stages decrypt or encrypt.
func (p *Processor) runOne(ctx *api.Context, run Stages, d document, password string, encryptionHandled bool, files map[string]string, outcome *remediationOutcome) (document, error) {
r, err := p.open(ctx, d.path, password)
r, err := p.open(ctx, ctx.Log(), d.path, password)
if err != nil {
return document{}, &stageError{op: run[0].op(), filename: d.filename, err: err}
}
@@ -682,7 +687,7 @@ func (p *Processor) runOne(ctx *api.Context, run Stages, d document, password st
}
}
env := &runEnv{processor: p, pageCount: pageCount, files: files, source: r}
env := &runEnv{processor: p, logger: ctx.Log(), pageCount: pageCount, files: files, source: r}
defer env.close()
pipeline := litho.NewPipeline().Open(r)
@@ -973,6 +978,8 @@ func documentPaths(docs []document) []string {
// pipeline has written.
type runEnv struct {
processor *Processor
// logger is the request's, which the documents the run opens log to.
logger *slog.Logger
pageCount int
files map[string]string
readers []*litho.Reader
@@ -1051,7 +1058,7 @@ func (env *runEnv) openFile(ctx context.Context, name string) (*litho.Reader, er
return nil, fmt.Errorf("file %q was not uploaded", name)
}
r, err := env.processor.open(ctx, path, "")
r, err := env.processor.open(ctx, env.logger, path, "")
if err != nil {
return nil, fmt.Errorf("file %q: %w", name, err)
}

View File

@@ -65,7 +65,7 @@ func (p *Processor) extractAttachmentsOne(ctx *api.Context, s *extractAttachment
span.End()
}()
r, err := p.open(spanCtx, d.path, password)
r, err := p.open(spanCtx, ctx.Log(), d.path, password)
if err != nil {
return nil, err
}

View File

@@ -54,7 +54,7 @@ func (p *Processor) renderOne(ctx *api.Context, s *renderStage, d document, pass
span.End()
}()
r, err := p.open(spanCtx, d.path, password)
r, err := p.open(spanCtx, ctx.Log(), d.path, password)
if err != nil {
return nil, err
}

View File

@@ -134,7 +134,7 @@ func readRoute(processor *Processor, name string, newRead func(form *api.FormDat
err = processor.forEachInputPathIndexed(ctx, inputPaths, func(i int, inputPath string) error {
filename := ctx.OriginalFilename(inputPath)
r, err := processor.open(ctx, inputPath, password)
r, err := processor.open(ctx, ctx.Log(), inputPath, password)
if err != nil {
return &stageError{op: name, filename: filename, err: err}
}

View File

@@ -57,7 +57,7 @@ func (p *Processor) extractTextOne(ctx *api.Context, s *extractTextStage, d docu
span.End()
}()
r, err := p.open(spanCtx, d.path, password)
r, err := p.open(spanCtx, ctx.Log(), d.path, password)
if err != nil {
return extractedFile{}, err
}

View File

@@ -40,6 +40,27 @@ Feature: /forms/pdf/{flatten-form|set-form|form}
Then the response status code should be 200
Then the response PDF(s) should be flatten
# PAYDAY stores an appearance that draws its old value; NAME is shown by two
# kid widgets that store none. Each widget shows the new value once
# flattened, in one pipe or in two calls.
Scenario: POST /forms/pdf/pipe (Set Form, Then Flatten Stored Appearances)
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/form_stale_appearance.pdf | file |
| stages | [{"op":"set-form","fields":{"PAYDAY":"MONDAY","NAME":"Jane Doe"}},{"op":"flatten-form"}] | field |
| Gotenberg-Output-Filename | foo | header |
Then the response status code should be 200
Then the response PDF(s) should be flatten
Then the "foo.pdf" PDF should have content matching "MONDAY" at page 1
Then the "foo.pdf" PDF should NOT have content matching "FRIDAY" at page 1
Then the "foo.pdf" PDF should have content matching "Jane Doe[\s\S]*Jane Doe" at page 1
When I make a "POST" request to Gotenberg at the "/forms/pdf/set-form" endpoint with the following form data and header(s):
| files | testdata/form_stale_appearance.pdf | file |
| fields | {"PAYDAY":"MONDAY","NAME":"Jane Doe"} | field |
| Gotenberg-Output-Filename | foo | header |
Then the response status code should be 200
Then the "foo.pdf" PDF should have the form field "PAYDAY" set to "MONDAY"
Scenario: POST /forms/pdf/set-form
Given I have a default Gotenberg container
When I make a "POST" request to Gotenberg at the "/forms/pdf/set-form" endpoint with the following form data and header(s):

View File

@@ -0,0 +1,134 @@
%PDF-1.3
%âãÏÓ
1 0 obj
<<
/Producer (pypdf)
>>
endobj
2 0 obj
<<
/Type /Pages
/Count 1
/Kids [ 4 0 R ]
>>
endobj
3 0 obj
<<
/Type /Catalog
/Pages 2 0 R
/AcroForm 11 0 R
>>
endobj
4 0 obj
<<
/Type /Page
/Resources <<
>>
/MediaBox [ 0.0 0.0 612 792 ]
/Parent 2 0 R
/Annots [ 7 0 R 9 0 R 10 0 R ]
>>
endobj
5 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
endobj
6 0 obj
<<
/Type /XObject
/Subtype /Form
/BBox [ 0.0 0.0 200 20 ]
/Resources <<
/Font <<
/Helv 5 0 R
>>
>>
/Length 56
>>
stream
/Tx BMC q BT /Helv 12 Tf 0 g 2 6 Td (FRIDAY) Tj ET Q EMC
endstream
endobj
7 0 obj
<<
/Type /Annot
/Subtype /Widget
/Rect [ 100 700 300 720 ]
/F 4
/P 4 0 R
/DA (\057Helv 12 Tf 0 g)
/FT /Tx
/T (PAYDAY)
/V (FRIDAY)
/AP <<
/N 6 0 R
>>
>>
endobj
8 0 obj
<<
/FT /Tx
/T (NAME)
/Kids [ 9 0 R 10 0 R ]
>>
endobj
9 0 obj
<<
/Type /Annot
/Subtype /Widget
/Rect [ 100 650 300 670 ]
/F 4
/P 4 0 R
/DA (\057Helv 12 Tf 0 g)
/Parent 8 0 R
>>
endobj
10 0 obj
<<
/Type /Annot
/Subtype /Widget
/Rect [ 100 600 300 620 ]
/F 4
/P 4 0 R
/DA (\057Helv 12 Tf 0 g)
/Parent 8 0 R
>>
endobj
11 0 obj
<<
/Fields [ 7 0 R 8 0 R ]
/DR <<
/Font <<
/Helv 5 0 R
>>
>>
/DA (\057Helv 12 Tf 0 g)
>>
endobj
xref
0 12
0000000000 65535 f
0000000015 00000 n
0000000054 00000 n
0000000113 00000 n
0000000179 00000 n
0000000304 00000 n
0000000401 00000 n
0000000603 00000 n
0000000770 00000 n
0000000832 00000 n
0000000962 00000 n
0000001093 00000 n
trailer
<<
/Size 12
/Root 3 0 R
/Info 1 0 R
>>
startxref
1198
%%EOF