phpdave11 / gofpdi

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

Problem importing one template PDF and saving it thousands of times #55

Open blakepatteson opened 2 years ago

blakepatteson commented 2 years ago

Hey - first off, just want to say I love this library, but I've been having an issue with importing the same PDF over and over again.

I have a static PDF file - "my_template_pdf.pdf". I want to import one (or both) pages from it multiple times, create a new PDF, add some text to it, and save the new PDF with a new name (basically, filling out forms).

I was just wondering if there's a way to do it in this library without creating a new importer for every output file? I'm interested in increasing performance.

Ideally I could just import the page(s) I need once, and then refer to those page(s) as I need them, but the only way I can get it to work is to create a new importer for every output file that I want to create.

func do_the_same_pdf_over_and_over(this_wg *sync.WaitGroup, i int) {
    defer this_wg.Done()
        this_output_form := gofpdf.New("L", "pt", "Letter", "")
        this_output_form.AddPage()

        this_importer := gofpdi.NewImporter()

        THIS_TEMPLATE_PDF := this_importer.ImportPage(this_output_form, "my_template_pdf.pdf", 1, "/MediaBox")
        this_importer.UseImportedTemplate(this_output_form, THIS_TEMPLATE_PDF , 0, 0, 792, 612)

        this_output_form.SetFont("Helvetica", "", 20)
        to_print := fmt.Sprintf("%v %v %v %v", "test print", "test", "person", i)
        this_output_form.Text(50, 50, to_print)
        this_output_path := fmt.Sprintf("test files/pdf output/output %v.pdf", to_print)
        err := this_output_form.OutputFileAndClose(this_output_path)

Basically I have to make a NewImporter() for every output file that I want, even if I'm using the same input template for 1000 output files.

If I don't make a new importer every time, then the file size is optimal for the first form, but every form after that the forms get larger and larger which is obviously bad for performance / memory. I can go into Adobe and do "Optimize PDF" and it removes a huge portion of "Document Overhead" and fixes the file size to be more or less optimal. So I think the library is saving some document overhead multiple times when I don't reinitialize the importer?

Is it by design that I should use a new importer for every output file that I want, even if I'm reusing the same template 1000 times? Am I just using the package incorrectly?