Skip to content

Commit

Permalink
Added Ability to turn template into a byte string
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Montag committed Nov 1, 2018
1 parent 6421d61 commit bb28751
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 3 deletions.
96 changes: 96 additions & 0 deletions def.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gofpdf

import (
"bytes"
"encoding/gob"
"io"
"time"
)
Expand Down Expand Up @@ -175,6 +176,101 @@ type ImageInfoType struct {
dpi float64
}

func (info *ImageInfoType) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)

err := encoder.Encode(info.data)
if err == nil {
err = encoder.Encode(info.smask)
}
if err == nil {
err = encoder.Encode(info.i)
}
if err == nil {
err = encoder.Encode(info.n)
}
if err == nil {
err = encoder.Encode(info.w)
}
if err == nil {
err = encoder.Encode(info.h)
}
if err == nil {
err = encoder.Encode(info.cs)
}
if err == nil {
err = encoder.Encode(info.pal)
}
if err == nil {
err = encoder.Encode(info.bpc)
}
if err == nil {
err = encoder.Encode(info.f)
}
if err == nil {
err = encoder.Encode(info.dp)
}
if err == nil {
err = encoder.Encode(info.trns)
}
if err == nil {
err = encoder.Encode(info.scale)
}
if err == nil {
err = encoder.Encode(info.dpi)
}

return w.Bytes(), err
}

func (info *ImageInfoType) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)

err := decoder.Decode(&info.data)
if err == nil {
err = decoder.Decode(&info.smask)
}
if err == nil {
err = decoder.Decode(&info.i)
}
if err == nil {
err = decoder.Decode(&info.n)
}
if err == nil {
err = decoder.Decode(&info.w)
}
if err == nil {
err = decoder.Decode(&info.h)
}
if err == nil {
err = decoder.Decode(&info.cs)
}
if err == nil {
err = decoder.Decode(&info.pal)
}
if err == nil {
err = decoder.Decode(&info.bpc)
}
if err == nil {
err = decoder.Decode(&info.f)
}
if err == nil {
err = decoder.Decode(&info.dp)
}
if err == nil {
err = decoder.Decode(&info.trns)
}
if err == nil {
err = decoder.Decode(&info.scale)
}
if err == nil {
err = decoder.Decode(&info.dpi)
}
return err
}

// PointConvert returns the value of pt, expressed in points (1/72 inch), as a
// value expressed in the unit of measure specified in New(). Since font
// management in Fpdf uses points, this method can help with line height
Expand Down
20 changes: 17 additions & 3 deletions fpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gofpdf_test
import (
"bufio"
"bytes"
"encoding/gob"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -1950,10 +1951,23 @@ 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)
}

pdf.AddPage()
pdf.UseTemplate(template)
pdf.UseTemplateScaled(template, gofpdf.PointType{X: 0, Y: 30}, tplSize)
pdf.UseTemplateScaled(template, gofpdf.PointType{X: 0, Y: 60}, tplSize.ScaleBy(1.4))
pdf.UseTemplate(template3)
pdf.UseTemplateScaled(template3, gofpdf.PointType{X: 0, Y: 30}, tplSize)
pdf.UseTemplateScaled(template3, gofpdf.PointType{X: 0, Y: 60}, tplSize.ScaleBy(1.4))
pdf.Line(40, 210, 60, 210)
pdf.Text(40, 200, "Template example page 1")

Expand Down
3 changes: 3 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gofpdf
*/

import (
"encoding/gob"
"sort"
)

Expand Down Expand Up @@ -107,6 +108,8 @@ type Template interface {
Bytes() []byte
Images() map[string]*ImageInfoType
Templates() []Template
gob.GobDecoder
gob.GobEncoder
}

func (f *Fpdf) templateFontCatalog() {
Expand Down
59 changes: 59 additions & 0 deletions template_impl.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package gofpdf

import (
"bytes"
"encoding/gob"
)

/*
* Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung),
* Marcus Downing, Jan Slabon (Setasign)
Expand Down Expand Up @@ -80,6 +85,60 @@ func (t *FpdfTpl) Templates() []Template {
return t.templates
}

func (t *FpdfTpl) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)

err := encoder.Encode(t.id)
if err == nil {
err = encoder.Encode(t.corner)
}
if err == nil {
err = encoder.Encode(t.size)
}
if err == nil {
err = encoder.Encode(t.bytes)
}
if err == nil {
err = encoder.Encode(t.images)
}
if err == nil {
err = encoder.Encode(t.templates)
}

return w.Bytes(), err
}

func (t *FpdfTpl) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)

err := decoder.Decode(&t.id)
if err == nil {
err = decoder.Decode(&t.corner)
}
if err == nil {
err = decoder.Decode(&t.size)
}
if err == nil {
err = decoder.Decode(&t.bytes)
}
if err == nil {
err = decoder.Decode(&t.images)
}
if err == nil {
templates := make([]*FpdfTpl, 0)
err = decoder.Decode(&templates)
t.templates = make([]Template, len(templates))

for x := 0; x < len(templates); x++ {
t.templates[x] = templates[x]
}
}

return err
}

// Tpl is an Fpdf used for writing a template. It has most of the facilities of
// an Fpdf, but cannot add more pages. Tpl is used directly only during the
// limited time a template is writable.
Expand Down

0 comments on commit bb28751

Please sign in to comment.