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

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

Closed
rorycl opened this issue Feb 21, 2020 · 4 comments
Closed

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

rorycl opened this issue Feb 21, 2020 · 4 comments

Comments

@rorycl
Copy link

rorycl commented Feb 21, 2020

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
Copy link
Owner

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
Copy link
Author

rorycl commented Feb 22, 2020

@phpdave11
Copy link
Owner

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

@phpdave11 phpdave11 reopened this Feb 22, 2020
@rorycl
Copy link
Author

rorycl commented Jun 14, 2020

That works great, thank you.

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

2 participants