phpdave11 / gofpdi

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

First page gets duplicated when using ImportPageFromStream #52

Closed thijsheijden closed 2 years ago

thijsheijden commented 2 years ago

Hi! Im trying to import multiple pages from a stream into a new PDF object. When using the following code:

tp := gofpdi.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
pdf.AddPage()
gofpdi.UseImportedTemplate(pdf, tp, 0, 0, paperWidth, 0)

I get the first page of the PDF perfectly fine. But when using a loop, or when adding a second page the same way as above (duplicating the code and changing the second import instance to page 2) the first page gets replaced by the second page, so the first two pages of the document are the second page of the document I am importing from. This same code works perfect when using ImportPage, so not sure why it is showing this strange behaviour when importing from a stream instead.

This code duplicates the first page, all other pages are fine:

for i := 1; i <= 13; i++ {
    tp := gofpdi.ImportPageFromStream(pdf, &rs, i, "/MediaBox")
    pdf.AddPage()
    gofpdi.UseImportedTemplate(pdf, tp, 0, 0, paperWidth, 0)
}
johan-lejdung commented 1 year ago

@thijsheijden I'm having the same issue, how did you solve it?

I can see in the source code that the writers n value, but I don't see a way to manipulate that.

johan-lejdung commented 1 year ago

I updated to latest version, as mentioned here: https://github.com/phpdave11/gofpdi/issues/33 but still isn't working correctly with this code. @phpdave11 what am I doing incorrectly?

importer := gofpdi.NewImporter()
for _, attachment := range msg.Attachments {
   rs := io.ReadSeeker(bytes.NewReader(attachment.Bytes))

   tplID := importer.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
   pdf.AddPage()

   pdf.SetFillColor(238, 238, 238)
   pdf.Rect(0, MarginTop, 210, ContentHeight-MarginTop, "F")

   importer.UseImportedTemplate(pdf, tplID, 0, MarginTop, 0, ContentHeight-MarginTop)
}

I'll be diving into debugging it tomorrow.

Edit: I opened another issue and PR to fix this https://github.com/phpdave11/gofpdi/issues/59