I am trying to develop a pdf generator. The pdf generator has multiple page. First page has some specific value with is working fine. But the second page has invoice which user uploads, In this case the user can upload a image file or pdf file.
For this I am using maroto library which works great when it comes to image or content generation but there is no support for import another pdf and merge with current one.
Now i know maroto uses gofpdf library and gofpdf has pdi importer so in my mind it should be possible to implement such feature. I didn't get any reply from their git issue board so asking it here.
Can anyone help me with this?? or my only choice is to change the library and do the coding again?
CodePudding user response:
I've created a simple library to help you with this. It adds two methods to the gofpdf Fpdf class:
importPdf
imports a PDF into the current PDF document
linkPdf
creates a hyperlink to a PDF document
It uses cgo to use the pdsys, pdflib and pdflib_pllibraries. You can find it here: https://github.com/jung-kurt/gofpdf
CodePudding user response:
So according to your reply i wanted to try it out. here is my code. so here pdf1byte is the first pdf and pdf2byte is second. both are 1 page pdf but pdf2 can have more than 1 page. so i am importing 1 page from pdf1 and then rest of pages should be pdf2. In this case the output is pdf2 in the both pages. If i comment out the pdf2 part of the import then output is pdf1 which is correct. In a nutshell after the whole code run i see page 1 is also changed as per pdf2's content. any idea?
pdf := gofpdf.New("P", "mm", "A4", "")
w, h := m.GetPageSize()
imp1 := gofpdi.NewImporter()
pdf1byte := io.ReadSeeker(bytes.NewReader(pdfm.Bytes()))
tpl1 := imp1.ImportPageFromStream(pdf, &pdf1byte, 1, "/MediaBox")
pdf.AddPage()
imp1.UseImportedTemplate(pdf, tpl1, 0, 0, w, h)
pdf2byte := io.ReadSeeker(bytes.NewReader(bikepass.Invoicebyte))
tpl1 = imp1.ImportPageFromStream(pdf, &pdf2byte, 1, "/MediaBox")
nrPages := len(imp1.GetPageSizes())
//add all pages from template pdf
for i := 1; i <= nrPages; i {
pdf.AddPage()
if i > 1 {
tpl1 = imp1.ImportPageFromStream(pdf, &rsinv, i, "/MediaBox")
}
imp1.UseImportedTemplate(pdf, tpl1, 0, 0, w, h)
}
err := pdf.OutputFileAndClose("pdfs/example.pdf")
if err != nil {
fmt.Errorf("error in generating the output pdf file: %w", err)
}```