2

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:

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()

Please help me to resolve this thing.

Thanks!

Karmdip Joshi

Karmdip joshi
  • 140
  • 2
  • 14
  • 1
    I am too facing a similar issue, I want to calculate blank pdf page size but as there is no xref information for the blank page I am getting error. but the code works when there is a pdf file having some info in it. – Parth Pandya Apr 25 '20 at 06:21

1 Answers1

1

you need to add SetSourceFile after you initialize importer

like imp.SetSourceFile("barcode.pdf")