phpdave11 / gofpdi

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

Getting errors when trying to use importers on certain pdfs #64

Open Lioncat2002 opened 9 months ago

Lioncat2002 commented 9 months ago

When I am trying to load a pdf using importer.SetSourceStream, I get the panic

panic: Failed to initialize parser: Failed to read pdf: Failed to to read pages: Failed to read kids: Failed to resolve page/pages object: Failed to read value for token: <<: Token is empty
goroutine 50 [running]:
runtime/debug.Stack()
        /usr/lib/go/src/runtime/debug/stack.go:24 +0x5e
github.com/gofiber/fiber/v2/middleware/recover.defaultStackTraceHandler(0x18?, {0x1007980?, 0xc000c86468})
        /home/kittycat/go/pkg/mod/github.com/gofiber/fiber/v2@v2.50.0/middleware/recover/recover.go:12 +0x26
github.com/LegalForceLawRAPC/notarysign/src/core/fiber.RunServer.New.func2.1()
        /home/kittycat/go/pkg/mod/github.com/gofiber/fiber/v2@v2.50.0/middleware/recover/recover.go:31 +0x72
panic({0x1007980?, 0xc000c86468?})
        /usr/lib/go/src/runtime/panic.go:914 +0x21f
github.com/phpdave11/gofpdi.(*Importer).SetSourceStream(0xc00094cf60, 0xc00041b790)
        /home/kittycat/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.14-0.20211212211723-1f10f9844311/importer.go:95 +0x1a8
github.com/LegalForceLawRAPC/notarysign/common/utils.AddPageToPDF({0xc0008be000, 0x247b6, 0x2a000}, 0x3)
        /run/media/kittycat/Linux_files/work/Signmarkia-Backend/common/utils/pdf_utils.go:91 +0x137
...

This is the code I wrote It just deletes a page from the pdf

func DeletePageFromPDF(pdfBuf []byte, pageNumber int) ([]byte, error) {

    pdf = InitGoPdf()
    importer := gofpdi.NewImporter()
    pdfReader := bytes.NewReader(pdfBuf)
    r := io.ReadSeeker(pdfReader)
    if r == nil {
        return nil, errors.New("failed to read pdf")
    }
    importer.SetSourceStream(&r)
    totalPagesOfPdf := importer.GetNumPages()

    for i := 0; i < totalPagesOfPdf; i++ {
        if pageNumber != i+1 {
            importedPage := pdf.ImportPageStream(&r, i+1, "/MediaBox")
            data := importer.GetPageSizes()
            pageWidth := data[i+1]["/MediaBox"]["w"]
            pageHeight := data[i+1]["/MediaBox"]["h"]
            pdf.AddPageWithOption(gopdf.PageOption{PageSize: &gopdf.Rect{W: pageWidth, H: pageHeight}})
            pdf.UseImportedTemplate(importedPage, 0, 0, pageWidth, pageHeight)
        }

    }

    var newPDFBuf bytes.Buffer
    _, err := pdf.WriteTo(&newPDFBuf)
    if err != nil {
        return nil, err
    }

    return newPDFBuf.Bytes(), nil
}

The issue seems to be happening randomly. It works once on the pdf and next time it fails on the same pdf

Here's a link to the entire code if needed https://gist.github.com/Lioncat2002/f40306aba45ccafe39dcceca294f50dc