pdfcpu / pdfcpu

A PDF processor written in Go.

Home Page:http://pdfcpu.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

runtime error: index out of range [0] with length 0

mdfgzh opened this issue · comments

I need to fill in Chinese characters into the form, but when I installed the font and filled it in, a panic error occurred. Is my usage method incorrect? Hope you can get the answer, thank you.

This is my code:

func TestFillFormFile(t *testing.T) {
	fontFiles := []string{"SIMSUN.ttf"}
	font.UserFontDir = "C:\\Users\\mdf\\Desktop\\git\\gitlab\\blockchain\\test\\font"
	err := api.InstallFonts(fontFiles)
	if err != nil {
		fmt.Println("An error occurred while installing fonts:", err)
		return
	}
	fmt.Println("Successful installation!")
	err = api.FillFormFile("ebCert.pdf", "ebCert.json", "ebCert_new.pdf", nil)
	if err != nil {
		println(err.Error())
		return
	}
}

This is log:

=== RUN   TestFillFormFile
Successful installation!
--- FAIL: TestFillFormFile (0.09s)
panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 6 [running]:
testing.tRunner.func1.2({0x56e7c0, 0xc00001f218})
	C:/Program Files/Go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
	C:/Program Files/Go/src/testing/testing.go:1548 +0x397
panic({0x56e7c0?, 0xc00001f218?})
	C:/Program Files/Go/src/runtime/panic.go:914 +0x21f
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/font.Lang(0xc0000282ac?, 0x6?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/font/fontDict.go:1087 +0x1b6
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.FormFontNameAndLangForID(0x0?, {0x18, 0x0})
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/font.go:155 +0xbe
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.extractFormFontDetails(0xc0001e0ea0, {0xc00001e5a1, 0x6}, 0x0?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/font.go:366 +0x8f
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.(*TextField).calcFontFromDA(0xc00035cfc0, 0xc0001e0ea0, 0x4?, 0x1, 0x0?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/textField.go:243 +0x26e
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.NewTextField(0xc0001e0ea0, 0x59f361?, {0xc00036b170, 0x9}, 0x0, 0x0, 0x0?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/textField.go:898 +0x318
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.renderTextFieldAP(0xc0001e0ea0, 0x59f361?, {0xc00036b170, 0x9}, 0xd0?, 0xedc85?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/textField.go:932 +0xad
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/primitives.EnsureTextFieldAP(0xc0001e0ea0, 0x59fda3?, {0xc00036b170, 0x9}, 0x0?, 0x592dc0?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/primitives/textField.go:955 +0x74c
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/form.fillTextField(0xc0001e0ea0, 0xc00036b280?, {0x5cbbf7?, 0xc000367750?}, {0xc00036b248?, 0x8?}, {0x0, 0x0}, 0x0, 0x1, ...)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/form/fill.go:950 +0x285
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/form.fillTx(0xc0001e0ea0, 0x9?, {0x5cbbf7, 0x2}, {0xc00036b248, 0x4}, 0xc0?, 0xc0001e1d40?, 0x5a0bb0?, 0xc000367d28, ...)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/form/fill.go:987 +0x1ee
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/form.fillWidgetAnnots(0xc0001e0ea0, {0xc000591d00, 0x9, 0x10}, 0x5?, {0xc0001df158?, 0xc0003083f0?}, 0xf1b?, 0x0?, 0xc000367d28, ...)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/form/fill.go:1050 +0x346
github.com/pdfcpu/pdfcpu/pkg/pdfcpu/form.FillForm(0xc0001e0ea0, 0x9?, 0x0, 0x0?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/pdfcpu/form/fill.go:1090 +0x20b
github.com/pdfcpu/pdfcpu/pkg/api.FillForm({0x660a18?, 0xc0003a4128?}, {0x65f240, 0xc0003a4120}, {0x65f200, 0xc0003a4130}, 0xc000112500?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/api/form.go:543 +0x32e
github.com/pdfcpu/pdfcpu/pkg/api.FillFormFile({0x5a2adc, 0xa}, {0x5a360a?, 0x1?}, {0x5a547d, 0xe}, 0x5ac?)
	C:/Users/mdf/go/pkg/mod/github.com/pdfcpu/pdfcpu@v0.7.0/pkg/api/form.go:606 +0x2fc
blockchain/test.TestFillFormFile(0x0?)
	C:/Users/mdf/Desktop/git/gitlab/blockchain/test/pdf_test.go:76 +0x148
testing.tRunner(0xc00005fd40, 0x5e7300)
	C:/Program Files/Go/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 1
	C:/Program Files/Go/src/testing/testing.go:1648 +0x3ad

Did you check if your pdf file is valid as far as pdfcpu is concerned?
pdfcpu val ebCert.pdf

Also I would not add a user font on the fly instead do it separately and ensure it can be loaded prior to using it using the API.

In any case if you can share your file I can take a look and provide a fix.

This is the simsun font used,This is a new one, not cropped
ebCert.pdf

This is a new document using Times Roman font,It can be filled correctly, but it does not support Chinese
ebCert.pdf

Hello!
Filling a form using a specific font is still an open issue.
See also #715 and #825