phpdave11 / gofpdi

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

panic: Failed to initialize parser: Failed to read pdf: Failed to read xref table #32

Closed codelikeakarma closed 4 years ago

codelikeakarma commented 4 years ago

I am getting the panic form gofpdi.importPage() for blank pdf if download from anywhere once I create the blank pdf using gofpdf then It's working fine

code is here:

pdf := gofpdf.New("P", "pt", "A4", "")
// create a new Importer instance
imp := gofpdi.NewImporter()

w,h := pdf.GetPageSize()

fmt.Println("pageSize >>>", w, ">>>>>", h)

// import first page and determine page sizes
tpl := imp.ImportPage(pdf, "barcode.pdf", 1, "/MediaBox")
pageSizes := imp.GetPageSizes()
nrPages := len(imp.GetPageSizes())

// add all pages from template pdf
for i := 1; i <= nrPages; i++ {
    pdf.AddPageFormat("P", gofpdf.SizeType{Wd: pageSizes[i]["/MediaBox"]["w"] , Ht:pageSizes[i]["/MediaBox"]["h"]})
    w,h := pdf.GetPageSize()
    fmt.Println("pageSize >>>", w, ">>>>>", h)
    if i > 1 {
        tpl = imp.ImportPage(pdf, "barcode.pdf", i, "/MediaBox")
    }
    imp.UseImportedTemplate(pdf, tpl, 0, 0, pageSizes[i]["/MediaBox"]["w"], pageSizes[i]["/MediaBox"]["h"])
}

// output
err := pdf.OutputFileAndClose("generated-barcode.pdf")
if err != nil {
    fmt.Println(err)
}

Here is an error I am getting while import page into pdf: Screenshot 2020-04-24 at 3 00 14 PM

And here it's blank pdf: barcode.pdf

This pdf I want to use for importPage()

@phpdave11 Please help me to resolve this thing.

Thanks!

Karmdip Joshi

phpdave11 commented 4 years ago

This is fixed in gofpdi v1.0.12

adits31 commented 4 years ago

@phpdave11 I'm getting a similar error as above, but with <= instead of =: Failed to initialize parser: Failed to read pdf: Failed to read xref table: Failed to read prev xref: Unsupported /DecodeParms - only tested with /Columns <= 4 and /Predictor <= 12.

I see in the latest version, the not equals check was switched to a greater than check - is there anyway to get around this or is it a necessary blocker? Any help would be appreciated, thanks!

phpdave11 commented 4 years ago

@adits31 if you have can provide the sample PDF that would be helpful so I can test it to make sure it works. You could try commenting out the line that triggers that error and see if it works.

codelikeakarma commented 4 years ago

Thanks @phpdave11, I tried in my sample demo code and it's working fine for me.

Again Thank you so much for your support.

codelikeakarma commented 4 years ago

Hello @adits31, If you don't mind I put my sample code you can try with this.

func main () {
    // create new pdf
    pdf := gofpdf.New("P", "mm", "A4", "")

    // I use the same function as the example on this repo
    rs, _ := getTemplatePdf()

    // create a new Importer instance
    imp := gofpdi.NewImporter()

    // import first page and determine page sizes
    tpl := imp.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
    pageSizes := imp.GetPageSizes()
    nrPages := len(imp.GetPageSizes())

    for i := 1; i <= nrPages; i++ {
        var sizeType gofpdf.SizeType
        sizeType.Wd = pageSizes[i]["/MediaBox"]["w"]
        sizeType.Ht = pageSizes[i]["/MediaBox"]["h"]
        tpl = imp.ImportPageFromStream(pdf, &rs, i, "/MediaBox")
        pdf.AddPageFormat("P", sizeType)
        imp.UseImportedTemplate(pdf, tpl, 0, 0, pageSizes[i]["/MediaBox"]["w"], pageSizes[i]["/MediaBox"]["h"])
    }
    fileStr := "OUTPUT.pdf"
    err := pdf.OutputFileAndClose(fileStr)

    if err != nil{
        fmt.Println("Error")
    }
}

func getTemplatePdf() (io.ReadSeeker, error) {
    file, err := os.Open("INPUT.pdf") // For read access.
    if err != nil {
        log.Fatal(err)
    }
    readSeek := io.ReadSeeker(file)
    return readSeek, nil
}
adits31 commented 4 years ago

@karmdip Thanks for the sample code! I can give that a shot and see if it works without removing the error check. I'm currently not using ImportPageFromStream.

@phpdave11 I commented out the line that triggers the error and it seems to work - is there a risk that I'd run into some downstream issue by ignoring that error or is it fine as long as the desired PDF is generating correctly? Thanks!

For reference, I'm doing something pretty simple like this:

pdf := gofpdf.New("P", "mm", "A4", "")
for i := 1; i < 5; i++ {
    tpl1 := gofpdi.ImportPage(pdf, inputPath, i, "/MediaBox")

    pdf.AddPage()

    // Draw imported template onto page
    gofpdi.UseImportedTemplate(pdf, tpl1, -10, -10, 225, 0)
}
codelikeakarma commented 4 years ago

@adits31 It's looking good.

But you need to update little bit and try it again

pdf := gofpdf.New("P", "mm", "A4", "")

tpl1 := gofpdi.ImportPage(pdf, inputPath, 1, "/MediaBox")

nrPages := len(imp.GetPageSizes())

for i := 1; i <= nrPages; i++ {
        tpl1 = gofpdi.ImportPage(pdf, inputPath, i, "/MediaBox")

        pdf.AddPage()

        // Draw imported template onto page
        gofpdi.UseImportedTemplate(pdf, tpl1, -10, -10, 225, 0)
}

If it's not working then please share your pdf.

I will check for this from my side

adits31 commented 4 years ago

@karmdip The code that I initially posted works when I comment out the check that's giving me the error - it seems like the PDF is generating correctly without it. I just want to check with @phpdave11 if removing that check would have adverse downstream effects. Specifically, this check was removed: https://github.com/phpdave11/gofpdi/blob/cf771f66a372da8380cb6c9fe5d9d40133dcc563/reader.go#L863

@karmdip In my case, I know that my PDF will always have 4 pages, which is why I'm hardcoding length - is there a reason why you moved the first ImportPage out of the loop? Also doesn't your example import the first page twice?

Thanks for the help!

codelikeakarma commented 4 years ago

@adits31 it's fine you are using Hard code but in my code first page not import twice.