Did anyone work with pdf merger from gofpdi library?
I am trying to merge 2 pdf as one file. I pass them as reedseeker.
unc test(pdfm bytes.Buffer, m pdf.Maroto, pdf2 bytes.Buffer) {
pdf := gofpdf.New("P", "mm", "A4", "")
w, h := m.GetPageSize()
importer := gofpdi.NewImporter()
rs := io.ReadSeeker(bytes.NewReader(pdfm.Bytes()))
tpl1 := importer.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
pdf.AddPage()
importer.UseImportedTemplate(pdf, tpl1, 0, 0, w, h)
importer2 := gofpdi.NewImporter()
rs2 := io.ReadSeeker(bytes.NewReader(pdf2.bytes()))
tpl2 := importer2.ImportPageFromStream(pdf, &rs2, 1, "/MediaBox")
pdf.AddPage()
importer.UseImportedTemplate(pdf, tpl2, 0, 0, w, h)
err := pdf.OutputFileAndClose("pdfs/example.pdf")
if err != nil {
fmt.Errorf("error in generating the output pdf file: %w", err)
}
}
As you can see tp1 and tp2 are imported from pdf1 and pdf2 so the final pdf should be merged. but in this case the final pdf has 2 page where both page contains the content of pdf2.
If i change the code to this,
func test(pdfm bytes.Buffer, m pdf.Maroto, pdf2 bytes.Buffer) {
pdf := gofpdf.New("P", "mm", "A4", "")
w, h := m.GetPageSize()
importer := gofpdi.NewImporter()
rs := io.ReadSeeker(bytes.NewReader(pdfm.Bytes()))
tpl1 := importer.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
pdf.AddPage()
importer.UseImportedTemplate(pdf, tpl1, 0, 0, w, h)
pdf.AddPage()
importer2 := gofpdi.NewImporter()
tpl2 := importer2.ImportPage(pdf, "test.pdf", 1, "/MediaBox")
pdf.AddPage()
importer.UseImportedTemplate(pdf, tpl2, 0, 0, w, h)
err := pdf.OutputFileAndClose("pdfs/example.pdf")
if err != nil {
fmt.Errorf("error in generating the output pdf file: %w", err)
}
}
then the final pdf has 1st page as pdf1 and second page as pdf2 which is correct. As you can see i have changed the second pdf import as file instead of byte stream.
what am i missing here?