Skip to content

Commit

Permalink
Prevent an importer from importing the same page more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
phpdave11 committed Jun 2, 2020
1 parent 938432d commit cf771f6
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit cf771f6

Please sign in to comment.