signintech / gopdf

A simple library for generating PDF written in Go lang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

invalid memory address or nil pointer dereference for Cell

sarithaN opened this issue · comments

Hi,
I am new to Go and gopdf.
When I am trying to create pdf just to print the "Hi! This is normal.", using below code.

    pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
pdf.AddPage()
    pdf.Cell(nil, "Hi! This is normal.")
pdf.Br(20)
pdf.Cell(nil, "Hi! This is italic.")
pdf.WritePdf("italic.pdf")

I am getting below exception.
[neerus@data-domain-dev-vm main]$ go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1e8 pc=0x4c4ae3]

goroutine 1 [running]:
github.com/signintech/gopdf.(*SubsetFontObj).AddChars(0x0, {0x4f8b39, 0x12})
../go/pkg/mod/github.com/signintech/gopdf@v0.16.1/subset_font_obj.go:139 +0xc3
github.com/signintech/gopdf.(*GoPdf).Cell(0xc000152000, 0x0?, {0x4f8b39, 0x12})
../go/pkg/mod/github.com/signintech/gopdf@v0.16.1/gopdf.go:1077 +0xb3
main.main()
../main/main.go:62 +0xb6

Please help me in How can I address this error

I guess because you haven't set the font yet.

pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})

//add ttf file and set font 
pdf.AddTTFFont("some", "some.ttf")
pdf.SetFont("some", "", 14)

pdf.AddPage()
pdf.Cell(nil, "Hi! This is normal.")
pdf.Br(20)
pdf.Cell(nil, "Hi! This is italic.")
pdf.WritePdf("italic.pdf")

Thanks it worked now

same issue. why not set the default TTF file? I did not found any document talk I must set the font value.