-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathfpdf_test.go
314 lines (274 loc) · 8.26 KB
/
fpdf_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright ©2023 The go-pdf Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
/*
* Copyright (c) 2013-2015 Kurt Jung (Gmail: kurt.w.jung)
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package fpdf_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/go-pdf/fpdf"
"github.com/go-pdf/fpdf/internal/example"
)
func init() {
cleanup()
}
func cleanup() {
_ = filepath.Walk(
example.PdfDir(),
func(path string, info os.FileInfo, err error) (reterr error) {
if info.Mode().IsRegular() {
dir, _ := filepath.Split(path)
if filepath.Base(dir) != "reference" {
if len(path) > 3 {
if path[len(path)-4:] == ".pdf" {
os.Remove(path)
}
}
}
}
return
},
)
}
var summaryCompare = example.SummaryCompare
func init() {
if runtime.GOOS == "windows" {
summaryCompare = example.Summary
}
}
func TestFpdfImplementPdf(t *testing.T) {
// this will not compile if Fpdf and Tpl
// do not implement Pdf
var _ fpdf.Pdf = (*fpdf.Fpdf)(nil)
var _ fpdf.Pdf = (*fpdf.Tpl)(nil)
}
// TestPagedTemplate ensures new paged templates work
func TestPagedTemplate(t *testing.T) {
pdf := fpdf.New("P", "mm", "A4", "")
tpl := pdf.CreateTemplate(func(t *fpdf.Tpl) {
// this will be the second page, as a page is already
// created by default
t.AddPage()
t.AddPage()
t.AddPage()
})
if tpl.NumPages() != 4 {
t.Fatalf("The template does not have the correct number of pages %d", tpl.NumPages())
}
tplPages := tpl.FromPages()
for x := 0; x < len(tplPages); x++ {
pdf.AddPage()
pdf.UseTemplate(tplPages[x])
}
// get the last template
tpl2, err := tpl.FromPage(tpl.NumPages())
if err != nil {
t.Fatal(err)
}
// the objects should be the exact same, as the
// template will represent the last page by default
// therefore no new id should be set, and the object
// should be the same object
if fmt.Sprintf("%p", tpl2) != fmt.Sprintf("%p", tpl) {
t.Fatal("Template no longer respecting initial template object")
}
}
// TestIssue0116 addresses issue 116 in which library silently fails after
// calling CellFormat when no font has been set.
func TestIssue0116(t *testing.T) {
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, "OK")
if pdf.Error() != nil {
t.Fatalf("not expecting error when rendering text")
}
pdf = fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.Cell(40, 10, "Not OK") // Font not set
if pdf.Error() == nil {
t.Fatalf("expecting error when rendering text without having set font")
}
}
// TestIssue0193 addresses issue 193 in which the error io.EOF is incorrectly
// assigned to the FPDF instance error.
func TestIssue0193(t *testing.T) {
var png []byte
var pdf *fpdf.Fpdf
var err error
var rdr *bytes.Reader
png, err = os.ReadFile(example.ImageFile("sweden.png"))
if err == nil {
rdr = bytes.NewReader(png)
pdf = fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
_ = pdf.RegisterImageOptionsReader("sweden", fpdf.ImageOptions{ImageType: "png", ReadDpi: true}, rdr)
err = pdf.Error()
}
if err != nil {
t.Fatalf("issue 193 error: %s", err)
}
}
// TestIssue0209SplitLinesEqualMultiCell addresses issue 209
// make SplitLines and MultiCell split at the same place
func TestIssue0209SplitLinesEqualMultiCell(t *testing.T) {
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 8)
// this sentence should not be splited
str := "Guochin Amandine"
lines := pdf.SplitLines([]byte(str), 26)
_, FontSize := pdf.GetFontSize()
y_start := pdf.GetY()
pdf.MultiCell(26, FontSize, str, "", "L", false)
y_end := pdf.GetY()
if len(lines) != 1 {
t.Fatalf("expect SplitLines split in one line")
}
if int(y_end-y_start) != int(FontSize) {
t.Fatalf("expect MultiCell split in one line %.2f != %.2f", y_end-y_start, FontSize)
}
// this sentence should be splited in two lines
str = "Guiochini Amandine"
lines = pdf.SplitLines([]byte(str), 26)
y_start = pdf.GetY()
pdf.MultiCell(26, FontSize, str, "", "L", false)
y_end = pdf.GetY()
if len(lines) != 2 {
t.Fatalf("expect SplitLines split in two lines")
}
if int(y_end-y_start) != int(FontSize*2) {
t.Fatalf("expect MultiCell split in two lines %.2f != %.2f", y_end-y_start, FontSize*2)
}
}
// TestFooterFuncLpi tests to make sure the footer is not call twice and SetFooterFuncLpi can work
// without SetFooterFunc.
func TestFooterFuncLpi(t *testing.T) {
pdf := fpdf.New("P", "mm", "A4", "")
var (
oldFooterFnc = "oldFooterFnc"
bothPages = "bothPages"
firstPageOnly = "firstPageOnly"
lastPageOnly = "lastPageOnly"
)
// This set just for testing, only set SetFooterFuncLpi.
pdf.SetFooterFunc(func() {
pdf.SetY(-15)
pdf.SetFont("Arial", "I", 8)
pdf.CellFormat(0, 10, oldFooterFnc,
"", 0, "C", false, 0, "")
})
pdf.SetFooterFuncLpi(func(lastPage bool) {
pdf.SetY(-15)
pdf.SetFont("Arial", "I", 8)
pdf.CellFormat(0, 10, bothPages, "", 0, "L", false, 0, "")
if !lastPage {
pdf.CellFormat(0, 10, firstPageOnly, "", 0, "C", false, 0, "")
} else {
pdf.CellFormat(0, 10, lastPageOnly, "", 0, "C", false, 0, "")
}
})
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
for j := 1; j <= 40; j++ {
pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j),
"", 1, "", false, 0, "")
}
if pdf.Error() != nil {
t.Fatalf("not expecting error when rendering text")
}
w := &bytes.Buffer{}
if err := pdf.Output(w); err != nil {
t.Errorf("unexpected err: %s", err)
}
b := w.Bytes()
if bytes.Contains(b, []byte(oldFooterFnc)) {
t.Errorf("not expecting %s render on pdf when FooterFncLpi is set", oldFooterFnc)
}
got := bytes.Count(b, []byte("bothPages"))
if got != 2 {
t.Errorf("footer %s should render on two page got:%d", bothPages, got)
}
got = bytes.Count(b, []byte(firstPageOnly))
if got != 1 {
t.Errorf("footer %s should render only on first page got: %d", firstPageOnly, got)
}
got = bytes.Count(b, []byte(lastPageOnly))
if got != 1 {
t.Errorf("footer %s should render only on first page got: %d", lastPageOnly, got)
}
f := bytes.Index(b, []byte(firstPageOnly))
l := bytes.Index(b, []byte(lastPageOnly))
if f > l {
t.Errorf("index %d (%s) should less than index %d (%s)", f, firstPageOnly, l, lastPageOnly)
}
}
func TestIssue0069PanicOnSplitTextWithUnicode(t *testing.T) {
var str string
defer func() {
if r := recover(); r != nil {
t.Errorf("%q make SplitText panic", str)
}
}()
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 8)
testChars := []string{"«", "»", "—"}
for _, str = range testChars {
_ = pdf.SplitText(str, 100)
}
}
func TestSplitTextHandleCharacterNotInFontRange(t *testing.T) {
var str string
defer func() {
if r := recover(); r != nil {
t.Errorf("%q text make SplitText panic", str)
}
}()
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 8)
// Test values in utf8 beyond the ascii range
// I assuming that if the function can handle values in this range
// it can handle others since the function basically use the rune codepoint
// as a index for the font char width and 1_000_000 elements must be
// enough (hopefully!) for the fonts used in the real world.
for i := 128; i < 1_000_000; i++ {
str = string(rune(i))
_ = pdf.SplitText(str, 100)
}
}
func BenchmarkLineTo(b *testing.B) {
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pdf.LineTo(170, 20)
}
}
func BenchmarkCurveTo(b *testing.B) {
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pdf.CurveTo(190, 100, 105, 100)
}
}