forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_test.go
114 lines (99 loc) · 2.83 KB
/
table_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
package gopdf_test
import (
"testing"
"github.com/signintech/gopdf"
)
func TestTable(t *testing.T) {
// Create a new PDF document
pdf := &gopdf.GoPdf{}
// Start the PDF with a custom page size (we'll adjust it later)
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 430, H: 200}})
// Add a new page to the document
pdf.AddPage()
err := pdf.AddTTFFont("LiberationSerif-Regular", "./test/res/LiberationSerif-Regular.ttf")
if err != nil {
t.Fatalf("Error loading font: %v", err)
return
}
err = pdf.SetFont("LiberationSerif-Regular", "", 11)
if err != nil {
t.Fatalf("Error set font: %v", err)
return
}
err = pdf.AddTTFFont("Ubuntu-L.ttf", "./examples/outline_example/Ubuntu-L.ttf")
if err != nil {
t.Fatalf("Error loading font: %v", err)
return
}
err = pdf.SetFont("Ubuntu-L.ttf", "", 11)
if err != nil {
t.Fatalf("Error set font: %v", err)
return
}
// Set the starting Y position for the table
tableStartY := 10.0
// Set the left margin for the table
marginLeft := 10.0
// Create a new table layout
table := pdf.NewTableLayout(marginLeft, tableStartY, 25, 5)
// Add columns to the table
table.AddColumn("CODE", 50, "left")
table.AddColumn("DESCRIPTION", 200, "left")
table.AddColumn("QTY.", 40, "right")
table.AddColumn("PRICE", 60, "right")
table.AddColumn("TOTAL", 60, "right")
// Add rows to the table
table.AddRow([]string{"001", "Product A", "2", "10.00", "20.00"})
table.AddRow([]string{"002", "Product B", "1", "15.00", "15.00"})
table.AddRow([]string{"003", "Product C", "3", "5.00", "15.00"})
// Set the style for table cells
table.SetTableStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Top: true,
Left: true,
Bottom: true,
Right: true,
Width: 1.0,
},
FillColor: gopdf.RGBColor{R: 255, G: 255, B: 255},
TextColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
FontSize: 10,
})
// Set the style for table header
table.SetHeaderStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Top: true,
Left: true,
Bottom: true,
Right: true,
Width: 2.0,
RGBColor: gopdf.RGBColor{R: 100, G: 150, B: 255},
},
FillColor: gopdf.RGBColor{R: 255, G: 200, B: 200},
TextColor: gopdf.RGBColor{R: 255, G: 100, B: 100},
Font: "Ubuntu-L.ttf",
FontSize: 12,
})
table.SetCellStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Right: true,
Bottom: true,
Width: 0.5,
RGBColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
},
FillColor: gopdf.RGBColor{R: 255, G: 255, B: 255},
TextColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
Font: "LiberationSerif-Regular",
FontSize: 10,
})
// Draw the table
err = table.DrawTable()
if err != nil {
t.Errorf("Error drawing table: %v", err)
}
// Save the PDF to the specified path
err = pdf.WritePdf("examples/table/example_table.pdf")
if err != nil {
t.Errorf("Error saving PDF: %v", err)
}
}