Skip to content

Panic in api.Create when text content value is an empty string #1235

@jolancornevin

Description

@jolancornevin

Hello,

First of all, thank you for this library!

I encountered an issue when generating a PDF from JSON input. If any of the text content elements have an empty string ("") as their value, pdfcpu/api.Create panics.
I have one obvious workaround, which is to replace all my empty strings with an empty space.

However, I wanted to report this since:

  • I couldn’t find an existing issue or documentation mentioning it.
  • It feels like an unexpected failure mode that could perhaps be handled more gracefully or just be ignored (of course, I don't know about the implementation details of pdfcpu, so it's probably not that simple).

I'm a minimal reproduction example

reader := bytes.NewReader([]byte(`{
	"paper": "A4",
	"crop": "10",
	"origin": "LowerLeft",
	"contentBox": false,
	"guides": false,
	"pages": {
		"1": {
			"content": {
				"text": [
					{
						"value":"",
						"anchor":"topright",
						"dx":0,
						"dy":-39,
						"font":{
							"name":"Helvetica",
							"size":10,
							"col":"Black"
						}
					}
				]
			}
		}
	}
}`))
file, err := os.Create("example.pdf")
if err != nil {
	panic(err)
}
defer file.Close()

var writer io.Writer = file

api.Create(nil, reader, writer, nil)

You'll be able to see that if you use "value":" ", (L12), there is no panic.

Here's an extract of my stacktrace:

panic({0x1033c2da0?, 0x1039983f0?})
	/usr/local/go/src/runtime/panic.go:792 +0x124
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model.horizontalWrapUp(0x0?, {0x0?, 0xa?}, 0x4082480000000000?, 0x0?, 0x140003a0bf0?, 0x9?, 0xa?, 0x0?, 0x0?, ...)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/model/text.go:419 +0xd8
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model.createBoundingBoxForColumn(_, _, _, _, _, {{0x0, 0x0}, {0x140003a0bf0, 0x9}, 0x0, ...}, ...)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/model/text.go:481 +0x2ac
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model.WriteColumn(_, {_, _}, _, _, {{0x0, 0x0}, {0x140003a0bf0, 0x9}, 0x0, ...}, ...)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/model/text.go:689 +0x32c
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model.WriteMultiLine(...)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/model/text.go:731
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model.WriteMultiLineAnchored(_, {_, _}, _, _, {{0x0, 0x0}, {0x140003a0bf0, 0x9}, 0x0, ...}, ...)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/model/text.go:766 +0x90
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*TextBox).render(0x1400013c3c0, 0x1400012f500, 0x1, 0x140003a8bd0)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/primitives/textBox.go:433 +0x2f0
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*Content).renderTextBoxes(0x1400017ed20, 0x1400012f500, 0x1, 0x140003a8bd0)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/primitives/content.go:1083 +0xc0
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*Content).renderPrimitives(0x1400017ed20, 0x1400012f500, 0x1, 0x140003a8bd0, 0x1400038f4e8)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/primitives/content.go:1251 +0x58
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*Content).render(0x1400017ed20, 0x1400012f500, 0x1, 0x140003a8bd0, 0x1400038f4e8)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/primitives/content.go:1309 +0x14c
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*PDF).RenderPages(0x1400017eb40)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/primitives/pdf.go:1127 +0x568
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/create.FromJSON(0x140002dc450, {0x10347a4a0, 0x140002dc0c0})
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/pdfcpu/create/create.go:744 +0xa0
github.com/pdfcpu/pdfcpu/pkg/api.Create({0x0?, 0x0?}, {0x10347a4a0, 0x140002dc0c0}, {0x10347a378, 0x14000066398}, 0x28?)
	$GOPATH/pkg/mod/github.com/pdfcpu/[email protected]/pkg/api/create.go:69 +0xcc

github.com/<anon>/anon.PDF.Render.func1()
	/app/anon/pdf.go:166 +0x3c

Hopefully it helps.
Let me know if I’ve missed any configuration detail or if there's a recommended way to handle this case. I’m happy to provide more details if needed.

Thanks in advance for your help 🙂

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions