From cf771f66a372da8380cb6c9fe5d9d40133dcc563 Mon Sep 17 00:00:00 2001 From: Dave Barnes Date: Tue, 2 Jun 2020 08:04:38 -0500 Subject: [PATCH] Prevent an importer from importing the same page more than once --- importer.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/importer.go b/importer.go index 2d11d12..a611936 100644 --- a/importer.go +++ b/importer.go @@ -7,12 +7,13 @@ import ( // The Importer class to be used by a pdf generation library type Importer struct { - sourceFile string - readers map[string]*PdfReader - writers map[string]*PdfWriter - tplMap map[int]*TplInfo - tplN int - writer *PdfWriter + sourceFile string + readers map[string]*PdfReader + writers map[string]*PdfWriter + tplMap map[int]*TplInfo + tplN int + writer *PdfWriter + importedPages map[string]int } type TplInfo struct { @@ -57,6 +58,7 @@ func (this *Importer) init() { this.writers = make(map[string]*PdfWriter, 0) this.tplMap = make(map[int]*TplInfo, 0) this.writer, _ = NewPdfWriter("") + this.importedPages = make(map[string]int, 0) } func (this *Importer) SetSourceFile(f string) { @@ -129,6 +131,12 @@ func (this *Importer) GetPageSizes() map[int]map[string]map[string]float64 { } func (this *Importer) ImportPage(pageno int, box string) int { + // If page has already been imported, return existing tplN + pageNameNumber := fmt.Sprintf("%s-%04d", this.sourceFile, pageno) + if _, ok := this.importedPages[pageNameNumber]; ok { + return this.importedPages[pageNameNumber] + } + res, err := this.GetWriter().ImportPage(this.GetReader(), pageno, box) if err != nil { panic(err) @@ -143,6 +151,9 @@ func (this *Importer) ImportPage(pageno int, box string) int { // Increment template id this.tplN++ + // Cache imported page tplN + this.importedPages[pageNameNumber] = tplN + return tplN }