forked from tiechui1994/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
141 lines (119 loc) · 3 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package gopdf
import (
"os"
"path/filepath"
"strings"
"github.com/tiechui1994/gopdf/core"
)
type Image struct {
pdf *core.Report
path string
width, height float64
margin core.Scope
tempFilePath string
}
func NewImage(path string, pdf *core.Report) *Image {
if _, err := os.Stat(path); err != nil {
panic("the path error")
}
var tempFilePath string
picturePath, _ := filepath.Abs(path)
imageType, _ := GetImageType(picturePath)
if imageType == "png" {
index := strings.LastIndex(picturePath, ".")
tempFilePath = picturePath[0:index] + ".jpeg"
err := ConvertPNG2JPEG(picturePath, tempFilePath)
if err != nil {
panic(err)
}
picturePath = tempFilePath
}
w, h := GetImageWidthAndHeight(picturePath)
image := &Image{
pdf: pdf,
path: picturePath,
width: float64(w / 10),
height: float64(h / 10),
tempFilePath: tempFilePath,
}
if tempFilePath != "" {
pdf.AddCallBack(image.delTempImage)
}
return image
}
func NewImageWithWidthAndHeight(path string, width, height float64, pdf *core.Report) *Image {
contentWidth, contentHeight := pdf.GetContentWidthAndHeight()
if width > contentWidth {
width = contentWidth
}
if height > contentHeight {
height = contentHeight
}
if _, err := os.Stat(path); err != nil {
panic("the path error")
}
var tempFilePath string
picturePath, _ := filepath.Abs(path)
imageType, _ := GetImageType(picturePath)
if imageType == "png" {
index := strings.LastIndex(picturePath, ".")
tempFilePath = picturePath[0:index] + ".jpeg"
err := ConvertPNG2JPEG(picturePath, tempFilePath)
if err != nil {
panic(err.Error())
}
picturePath = tempFilePath
}
w, h := GetImageWidthAndHeight(picturePath)
if float64(h)*width/float64(w) > height {
width = float64(w) * height / float64(h)
} else {
height = float64(h) * width / float64(w)
}
image := &Image{
pdf: pdf,
path: picturePath,
width: width,
height: height,
tempFilePath: tempFilePath,
}
if tempFilePath != "" {
pdf.AddCallBack(image.delTempImage)
}
return image
}
func (image *Image) SetMargin(margin core.Scope) *Image {
margin.ReplaceMarign()
image.margin = margin
return image
}
func (image *Image) GetHeight() float64 {
return image.height
}
func (image *Image) GetWidth() float64 {
return image.width
}
// 自动换行
func (image *Image) GenerateAtomicCell() error {
var (
sx, sy = image.pdf.GetXY()
)
x, y := sx+image.margin.Left, sy+image.margin.Top
_, pageEndY := image.pdf.GetPageEndXY()
if y < pageEndY && y+float64(image.height) > pageEndY {
image.pdf.AddNewPage(false)
}
image.pdf.Image(image.path, x, y, x+float64(image.width), y+float64(image.height))
sx, _ = image.pdf.GetPageStartXY()
image.pdf.SetXY(sx, y+float64(image.height)+image.margin.Bottom)
return nil
}
func (image *Image) delTempImage(report *core.Report) {
if image.tempFilePath == "" {
return
}
if _, err := os.Stat(image.tempFilePath); err != nil {
return
}
os.Remove(image.tempFilePath)
}