phpdave11 / gofpdi

Go Free PDF Document Importer
MIT License
119 stars 59 forks source link

Error while inserting a random PDF to another PDF #61

Open Vishalj32 opened 1 year ago

Vishalj32 commented 1 year ago

I am getting a nil pointer error on resolveObjects method while I am inserting an existing pdf to a new pdf

package main

import "github.com/signintech/gopdf"

func main() {
    gPdf := gopdf.GoPdf{}
    gPdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) //595.28, 841.89 = A4

    pdf := gPdf.ImportPage("/tmp/documents/agreement.pdf", 1, "/MediaBox")
    gPdf.UseImportedTemplate(pdf, 0, 0, 150, 0)

    _ = gPdf.WritePdf("/tmp/documents/see-all.pdf")
}

StackTrace:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1003d8a5c]

goroutine 1 [running]:
github.com/phpdave11/gofpdi.(*PdfReader).resolveObject(0x140000b4210, 0x0)
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/reader.go:599 +0x13c
github.com/phpdave11/gofpdi.(*PdfReader).readPages(0x140000b4210)
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/reader.go:1228 +0x4c
github.com/phpdave11/gofpdi.(*PdfReader).read(0x140000b4210)
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/reader.go:1622 +0x4c
github.com/phpdave11/gofpdi.(*PdfReader).init(0x140000b4210)
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/reader.go:75 +0x11c
github.com/phpdave11/gofpdi.NewPdfReader({0x1003f9d75, 0x3d})
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/reader.go:61 +0xc4
github.com/phpdave11/gofpdi.(*Importer).SetSourceFile(0x1400009e040, {0x1003f9d75?, 0x5000?})
        /Users/vishaljain/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/importer.go:69 +0x80
github.com/signintech/gopdf.(*GoPdf).ImportPage(0x140000cc000, {0x1003f9d75?, 0x0?}, 0x0?, {0x1003f0cbc, 0x9})
        /Users/vishaljain/go/pkg/mod/github.com/signintech/gopdf@v0.18.0/gopdf.go:1387 +0x40
main.main()
        /Users/vishaljain/go/src/pdftester/main.go:9 +0xcc
exit status 2

If we can add a nil check on the method it would be really helpful, on this specific file

Brindrajsinh-Chauhan commented 1 year ago

Facing the same issue. An solutions?

0x-1 commented 8 months ago

seems to be fixed by this code in a fork: https://github.com/phpdave11/gofpdi/commit/7591c1ffd5b41753b4591967abc34ff504da34d3 https://github.com/happyreturns/gofpdi