phpdave11 / gofpdi

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

Failed to get page resources: Page <n> does not exist!! #26

Closed rorycl closed 4 years ago

rorycl commented 4 years ago

The following code produces an error on 1.0.11:

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/example4.pdf"

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*MM_TO_RMPOINTS)

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

The error is as follows:

Example PDF from http://www.campbell-lange.net/media/files/example4.pdf
file: /tmp/pdfexample-051294996
   pages:  50
   page :   1 processed
   page :   2 processed
   page :   3 processed
   page :   4 processed
   page :   5 processed
   page :   6 processed
   page :   7 processed
panic: Failed to get page resources: Page 8 does not exist!!

goroutine 1 [running]:
github.com/phpdave11/gofpdi.(*Importer).ImportPage(0xc000079440, 0x8, 0x793158, 0x9, 0x0)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.11/importer.go:124 +0x366
github.com/phpdave11/gofpdf/contrib/gofpdi.(*Importer).getTemplateID(0xc0000a4560, 0x81b0a0, 0xc000224000, 0x8, 0x793158, 0x9, 0x7)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.1-0.20200211150905-48c1f7f2764c/contrib/gofpdi/gofpdi.go:61 +0x50
github.com/phpdave11/gofpdf/contrib/gofpdi.(*Importer).ImportPage(0xc0000a4560, 0x81b0a0, 0xc000224000, 0xc000196100, 0x19, 0x8, 0x793158, 0x9, 0x0)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.1-0.20200211150905-48c1f7f2764c/contrib/gofpdi/gofpdi.go:45 +0x89
github.com/phpdave11/gofpdf/contrib/gofpdi.ImportPage(...)
    /home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.1-0.20200211150905-48c1f7f2764c/contrib/gofpdi/gofpdi.go:115
main.main()
    /home/rory/src/go-gofpdi-test/gofpdi-t3-web.go:64 +0x402
exit status 2
phpdave11 commented 4 years ago

Thanks, @rorycl! I believe I have to add support for reading page tree hierarchies in the readPages function. Currently the code only reads the top level.

rorycl commented 4 years ago

Hi @phpdave11 -- thanks for your response.

Does https://github.com/pdfcpu/pdfcpu/blob/6d5c8c0503790560df905e9bed9aff8b59232e79/pkg/pdfcpu/optimize.go#L551 help at all?

phpdave11 commented 4 years ago

@rorycl I have fixed this issue and I will create a new release once I do more testing.

rorycl commented 4 years ago

That works great, thank you.