Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lioncat2002 opened this issue Feb 18, 2024 · 0 comments
Open

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

Lioncat2002 opened this issue Feb 18, 2024 · 0 comments

Comments

@Lioncat2002
Copy link

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/[email protected]/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/[email protected]/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/[email protected]/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant