summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fpdf_test.go16
-rw-r--r--template.go1
-rw-r--r--template_impl.go17
3 files changed, 21 insertions, 13 deletions
diff --git a/fpdf_test.go b/fpdf_test.go
index e212d95..19bc533 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -19,7 +19,6 @@ package gofpdf_test
import (
"bufio"
"bytes"
- "encoding/gob"
"fmt"
"io"
"io/ioutil"
@@ -1951,18 +1950,9 @@ func ExampleFpdf_CreateTemplate() {
pdf.SetLineWidth(2.5)
pdf.SetFont("Arial", "B", 16)
- template3 := new(gofpdf.FpdfTpl)
- b := new(bytes.Buffer)
- enc := gob.NewEncoder(b)
-
- if err := enc.Encode(template); err != nil {
- pdf.SetError(err)
- }
-
- dec := gob.NewDecoder(b)
- if err := dec.Decode(template3); err != nil {
- pdf.SetError(err)
- }
+ // serialize and deserialize template
+ b, _ := template2.Serialize()
+ template3, _ := gofpdf.DeserializeTemplate(b)
pdf.AddPage()
pdf.UseTemplate(template3)
diff --git a/template.go b/template.go
index 2b886ed..9abdf62 100644
--- a/template.go
+++ b/template.go
@@ -108,6 +108,7 @@ type Template interface {
Bytes() []byte
Images() map[string]*ImageInfoType
Templates() []Template
+ Serialize() ([]byte, error)
gob.GobDecoder
gob.GobEncoder
}
diff --git a/template_impl.go b/template_impl.go
index 001b2d9..9c690c1 100644
--- a/template_impl.go
+++ b/template_impl.go
@@ -86,6 +86,23 @@ func (t *FpdfTpl) Templates() []Template {
return t.templates
}
+// Turn a template into a byte string for later deserialization
+func (t *FpdfTpl) Serialize() ([]byte, error) {
+ b := new(bytes.Buffer)
+ enc := gob.NewEncoder(b)
+ err := enc.Encode(t)
+
+ return b.Bytes(), err
+}
+
+// Create a template from a previously serialized template
+func DeserializeTemplate(b []byte) (Template, error) {
+ tpl := new(FpdfTpl)
+ dec := gob.NewDecoder(bytes.NewBuffer(b))
+ err := dec.Decode(tpl)
+ return tpl, err
+}
+
// returns the next layer of children images, it doesn't dig into
// children of children. Applies template namespace to keys to ensure
// no collisions. See UseTemplateScaled