summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--def.go2
-rw-r--r--fpdf.go9
-rw-r--r--fpdf_test.go6
-rw-r--r--util.go34
4 files changed, 48 insertions, 3 deletions
diff --git a/def.go b/def.go
index 437449c..f1c8d7b 100644
--- a/def.go
+++ b/def.go
@@ -160,6 +160,8 @@ type Fpdf struct {
page int // current page number
n int // current object number
offsets []int // array of object offsets
+ templates map[int64]Template // templates used in this document
+ templateObjects map[int64]int //template object IDs within this document
buffer fmtBuffer // buffer holding in-memory PDF
pages []*bytes.Buffer // slice[page] of page content; 1-based
state int // current document state
diff --git a/fpdf.go b/fpdf.go
index 22de11d..dfd721f 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -72,6 +72,8 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType)
f.fonts = make(map[string]fontDefType)
f.fontFiles = make(map[string]fontFileType)
f.diffs = make([]string, 0, 8)
+ f.templates = make(map[int64]Template)
+ f.templateObjects = make(map[int64]int)
f.images = make(map[string]*ImageInfoType)
f.pageLinks = make([][]linkType, 0, 8)
f.pageLinks = append(f.pageLinks, make([]linkType, 0, 0)) // pageLinks[0] is unused (1-based)
@@ -3223,6 +3225,12 @@ func (f *Fpdf) putxobjectdict() {
// foreach($this->images as $image)
f.outf("/I%d %d 0 R", image.i, image.n)
}
+ for _, tpl := range f.templates {
+ id := tpl.ID()
+ if objID, ok := f.templateObjects[id]; ok {
+ f.outf("/TPL%d %d 0 R", id, objID)
+ }
+ }
}
func (f *Fpdf) putresourcedict() {
@@ -3305,6 +3313,7 @@ func (f *Fpdf) putresources() {
return
}
f.putimages()
+ f.putTemplates()
// Resource dictionary
f.offsets[2] = f.buffer.Len()
f.out("2 0 obj")
diff --git a/fpdf_test.go b/fpdf_test.go
index c21b1ad..e4b8df4 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -29,7 +29,7 @@ import (
"strconv"
"strings"
- "github.com/marcusatbang/gofpdf"
+ "github.com/jung-kurt/gofpdf"
)
// Absolute path needed for gocov tool; relative OK for test
@@ -1733,9 +1733,9 @@ func ExampleFpdf_CreateTemplate() {
pdf.Line(60, 210, 80, 210)
pdf.Text(40, 200, "Template example page 2")
- fileStr := exampleFilename("template")
+ fileStr := exampleFilename("Fpdf_CreateTemplate")
err := pdf.OutputFileAndClose(fileStr)
summary(err, fileStr)
// Output:
- // Successfully generated pdf/template.pdf
+ // Successfully generated pdf/Fpdf_CreateTemplate.pdf
}
diff --git a/util.go b/util.go
index ce391f5..11cb016 100644
--- a/util.go
+++ b/util.go
@@ -278,3 +278,37 @@ func (f *Fpdf) UnicodeTranslatorFromDescriptor(cpStr string) (rep func(string) s
}
return
}
+
+// Transform moves a point by given X, Y offset
+func (p *PointType) Transform(x, y float64) PointType {
+ return PointType{p.X + x, p.Y + y}
+}
+
+// Orientation returns the orientation of a given size:
+// "P" for portrait, "L" for landscape
+func (s *SizeType) Orientation() string {
+ if s == nil || s.Ht == s.Wd {
+ return ""
+ }
+ if s.Wd > s.Ht {
+ return "L"
+ }
+ return "P"
+}
+
+// ScaleBy expands a size by a certain factor
+func (s *SizeType) ScaleBy(factor float64) SizeType {
+ return SizeType{s.Wd * factor, s.Ht * factor}
+}
+
+// ScaleToWidth adjusts the height of a size to match the given width
+func (s *SizeType) ScaleToWidth(width float64) SizeType {
+ height := s.Ht * width / s.Wd
+ return SizeType{width, height}
+}
+
+// ScaleToHeight adjsuts the width of a size to match the given height
+func (s *SizeType) ScaleToHeight(height float64) SizeType {
+ width := s.Wd * height / s.Ht
+ return SizeType{width, height}
+}