phpdave11/gofpdi

panic: unexpected EOF with github.com/phpdave11/gofpdi@v1.0.9

Closed this issue · 4 comments

The following code:

package main

import (
	"github.com/phpdave11/gofpdf"
	"github.com/phpdave11/gofpdf/contrib/gofpdi"

	"io"
	"io/ioutil"
	"net/http"
	"os"
)

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

var url string = "http://www.campbell-lange.net/media/files/example.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)
	}
	defer os.Remove(tmpFile.Name())

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

	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},
	})

	pdf.AddPage()
	var importPage = 1

	bgpdf := gofpdi.ImportPage(pdf, tmpFile.Name(), importPage, "/MediaBox")
	gofpdi.UseImportedTemplate(pdf, bgpdf, 0, 0, 210*MM_TO_RMPOINTS, 297*MM_TO_RMPOINTS)

}

Fails with

panic: unexpected EOF

goroutine 1 [running]:
github.com/phpdave11/gofpdi.(*PdfReader).readXref(0xc0001a40a0, 0x0, 0x0)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.9/reader.go:976 +0x2670
github.com/phpdave11/gofpdi.(*PdfReader).read(0xc0001a40a0, 0xc00018e050, 0x40dd48)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.9/reader.go:1518 +0x50
github.com/phpdave11/gofpdi.(*PdfReader).init(0xc0001a40a0, 0xc0001a40a0, 0xc0001a20d0)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.9/reader.go:72 +0xc2
github.com/phpdave11/gofpdi.NewPdfReader(0xc0001760a0, 0x19, 0xc0001760a0, 0x19, 0xa23d00)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.9/reader.go:58 +0x116
github.com/phpdave11/gofpdi.(*Importer).SetSourceFile(0xc0000af400, 0xc0001760a0, 0x19)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdi@v1.0.9/importer.go:67 +0x166
github.com/phpdave11/gofpdf/contrib/gofpdi.(*Importer).ImportPage(0xc0000a0560, 0x7fa600, 0xc00019a000, 0xc0001760a0, 0x19, 0x1, 0x77fad8, 0x9, 0x4083b88b1c22f64f)
	/home/rory/go/pkg/mod/github.com/phpdave11/gofpdf@v1.4.1-0.20200211150905-48c1f7f2764c/contrib/gofpdi/gofpdi.go:43 +0x46
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-t.go:48 +0x37b

Apologies if I've done something stupid with my code.

The example PDF is in the url referenced in the snippet. It is a PDF compiled from a .tex file using pdflatex.

@rorycl thank you for the detailed bug report. It may have something to do with the new XRef Stream parser code in v1.0.9. I will look into this more tonight.

@phpdave11 - thanks very much indeed for your efforts.

I get a different, panic: did not set root object error with http://www.campbell-lange.net/media/files/example2.pdf, which is made from a Google Docs PDF conversion.

It's possible my example code isn't a useful. Let me know if so.

@rorycl I have determined the cause of the unexpected EOF error and I am working on a fix.

This has been resolved in gofpdi v1.0.10.