forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopdf_test.go
134 lines (112 loc) · 2.18 KB
/
gopdf_test.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
package gopdf
import (
"io/ioutil"
"log"
"os"
"testing"
)
func BenchmarkPdfWithImageHolder(b *testing.B) {
err := initTesting()
if err != nil {
b.Error(err)
return
}
pdf := GoPdf{}
pdf.Start(Config{PageSize: Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
pdf.AddPage()
err = pdf.AddTTFFont("loma", "./test/res/times.ttf")
if err != nil {
b.Error(err)
return
}
err = pdf.SetFont("loma", "", 14)
if err != nil {
log.Print(err.Error())
return
}
bytesOfImg, err := ioutil.ReadFile("./test/res/chilli.jpg")
if err != nil {
b.Error(err)
return
}
imgH, err := ImageHolderByBytes(bytesOfImg)
if err != nil {
b.Error(err)
return
}
for i := 0; i < b.N; i++ {
pdf.ImageByHolder(imgH, 20.0, float64(i)*2.0, nil)
}
pdf.SetX(250)
pdf.SetY(200)
pdf.Cell(nil, "gopher and gopher")
pdf.WritePdf("./test/out/image_bench.pdf")
}
func initTesting() error {
err := os.MkdirAll("./test/out", 0777)
if err != nil {
return err
}
return nil
}
func TestPdfWithImageHolder(t *testing.T) {
err := initTesting()
if err != nil {
t.Error(err)
return
}
pdf := GoPdf{}
pdf.Start(Config{PageSize: Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
pdf.AddPage()
err = pdf.AddTTFFont("loma", "./test/res/times.ttf")
if err != nil {
t.Error(err)
return
}
err = pdf.SetFont("loma", "", 14)
if err != nil {
log.Print(err.Error())
return
}
bytesOfImg, err := ioutil.ReadFile("./test/res/PNG_transparency_demonstration_1.png")
if err != nil {
t.Error(err)
return
}
imgH, err := ImageHolderByBytes(bytesOfImg)
if err != nil {
t.Error(err)
return
}
err = pdf.ImageByHolder(imgH, 20.0, 20, nil)
if err != nil {
t.Error(err)
return
}
err = pdf.ImageByHolder(imgH, 20.0, 200, nil)
if err != nil {
t.Error(err)
return
}
pdf.SetX(250)
pdf.SetY(200)
pdf.Cell(nil, "gopher and gopher")
pdf.WritePdf("./test/out/image_test.pdf")
}
/*
func TestBuffer(t *testing.T) {
b := bytes.NewReader([]byte("ssssssss"))
b1, err := ioutil.ReadAll(b)
if err != nil {
t.Error(err)
return
}
fmt.Printf("->%s\n", string(b1))
b.Seek(0, 0)
b2, err := ioutil.ReadAll(b)
if err != nil {
t.Error(err)
return
}
fmt.Printf("+>%s\n", string(b2))
}*/