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

First page gets duplicated when using ImportPageFromStream #52

Closed
thijsheijden opened this issue May 20, 2022 · 2 comments
Closed

First page gets duplicated when using ImportPageFromStream #52

thijsheijden opened this issue May 20, 2022 · 2 comments

Comments

@thijsheijden
Copy link

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

@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
Copy link
Contributor

johan-lejdung commented Mar 8, 2023

I updated to latest version, as mentioned here: #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 #59

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