-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
image.go
78 lines (65 loc) · 2.38 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package internal
import (
"bytes"
"encoding/base64"
"errors"
"github.com/google/uuid"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/props"
"github.com/jung-kurt/gofpdf"
)
// Image is the abstraction which deals of how to add images in a PDF
type Image interface {
AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect) (err error)
AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect, extension consts.Extension) (err error)
}
type image struct {
pdf gofpdf.Pdf
math Math
}
// NewImage create an Image
func NewImage(pdf gofpdf.Pdf, math Math) *image {
return &image{
pdf,
math,
}
}
// AddFromFile open an image from disk and add to PDF
func (s *image) AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect) error {
info := s.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{
ReadDpi: false,
ImageType: "",
})
if info == nil {
return errors.New("Could not register image options, maybe path/name is wrong")
}
s.addImageToPdf(path, info, marginTop, qtdCols, colHeight, indexCol, prop)
return nil
}
// AddFromBase64 use a base64 string to add to PDF
func (s *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect, extension consts.Extension) error {
imageId, _ := uuid.NewRandom()
ss, _ := base64.StdEncoding.DecodeString(b64)
info := s.pdf.RegisterImageOptionsReader(
imageId.String(),
gofpdf.ImageOptions{
ReadDpi: false,
ImageType: string(extension),
},
bytes.NewReader(ss),
)
if info == nil {
return errors.New("Could not register image options, maybe path/name is wrong")
}
s.addImageToPdf(imageId.String(), info, marginTop, qtdCols, colHeight, indexCol, prop)
return nil
}
func (s *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, marginTop, qtdCols, colHeight, indexCol float64, prop props.Rect) {
var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, prop.Percent)
} else {
x, y, w, h = s.math.GetRectNonCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, prop)
}
s.pdf.Image(imageLabel, x, y+marginTop, w, h, false, "", 0, "")
}