phpdave11 / gofpdi

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

Failed to read xref table: Failed to read prev xref: Unsupported /DecodeParms #36

Open rorycl opened 4 years ago

rorycl commented 4 years ago

Hi @phpdave11

I'm getting the following error on 1.0.13:

panic: 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 get this error with a number of large PDFs (such as books) and PDFs made with Google Docs.

How to reproduce:

package main

import (
    "fmt"
    "github.com/phpdave11/gofpdf"
    "github.com/phpdave11/gofpdf/contrib/gofpdi"
    "io"
    "io/ioutil"
    "net/http"
    "os"
    rpdf "rsc.io/pdf"
)

const PDF_WIDTH_IN_MM = 222.6264
const PDF_HEIGHT_IN_MM = 297.0000
const MM_TO_RMPOINTS = 2.83465

var Verbose bool = true

var url string = "http://www.campbell-lange.net/media/files/example_2020a.pd

func main() {

    resp, err := http.Get(url)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    tmpFile, err := ioutil.TempFile(os.TempDir(), "pdfexample-")
    if err != nil {
        panic(err)
    }
    filer := tmpFile.Name()
    defer os.Remove(filer)

    _, err = io.Copy(tmpFile, resp.Body)
    if err != nil {
        panic(err)
    }

    // get pdf file information
    fi, err := rpdf.Open(filer)
    if err != nil {
        panic(err)
    }
    pageNum := fi.NumPage()
    if Verbose {
        fmt.Printf("Example PDF from %s\n", url)
        fmt.Printf("file: %s\n   pages: %3d\n", filer, pageNum)
    }

    // construct new gofpdf document
    pdf := gofpdf.NewCustom(&gofpdf.InitType{
        UnitStr: "pt",
        Size: gofpdf.SizeType{
            Wd: PDF_WIDTH_IN_MM * MM_TO_RMPOINTS,
            Ht: PDF_HEIGHT_IN_MM * MM_TO_RMPOINTS},
    })

    for p := 1; p <= pageNum; p++ {
        pdf.AddPage()

        bgpdf := gofpdi.ImportPage(pdf, filer, p, "/MediaBox")
        gofpdi.UseImportedTemplate(pdf, bgpdf, 0, 0, 210*MM_TO_RMPOINTS, 297

        if Verbose {
            fmt.Printf("   page : %3d processed\n", p)
        }
    }
}

Thanks for your efforts! Rory

Ayfel commented 4 years ago

Finding this same issue as well as the "panic: runtime error: invalid memory address or nil pointer dereference" with differents pdfs

rorycl commented 3 years ago

Actually this now errors differently for me!

The errors are:

panic: 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

Please see below

go run main.go 
go: finding module for package github.com/phpdave11/gofpdf
go: finding module for package rsc.io/pdf
go: finding module for package github.com/phpdave11/gofpdf/contrib/gofpdi
go: found github.com/phpdave11/gofpdf in github.com/phpdave11/gofpdf v1.4.2
go: found github.com/phpdave11/gofpdf/contrib/gofpdi in github.com/phpdave11/gofpdf v1.4.2
go: found rsc.io/pdf in rsc.io/pdf v0.1.1
go: downloading github.com/phpdave11/gofpdi v1.0.12
Example PDF from http://www.campbell-lange.net/media/files/example_2020a.pdf
file: /tmp/pdfexample-077686676
   pages:   3
panic: 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

goroutine 1 [running]:
github.com/phpdave11/gofpdi.(*Importer).SetSourceFile(0xc000139740, 0xc00028a0a0, 0x19)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.12/importer.go:69 +0x219
github.com/phpdave11/gofpdf/contrib/gofpdi.(*Importer).ImportPage(0xc00011c590, 0x7f0620, 0xc0002dc000, 0xc00028a0a0, 0x19, 0x1, 0x77574a, 0x9, 0x4083b88b1c22f64f)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.2/contrib/gofpdi/gofpdi.go:43 +0x46
github.com/phpdave11/gofpdf/contrib/gofpdi.ImportPage(...)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.2/contrib/gofpdi/gofpdi.go:115
main.main()
    /tmp/s/main.go:65 +0x3cc
exit status 2

By the way I've stuck the test file in the attachment since I've failed to paste it all in above. main.go.txt

0x-1 commented 2 years ago

see #16