-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.go
More file actions
106 lines (87 loc) · 2.86 KB
/
pdf.go
File metadata and controls
106 lines (87 loc) · 2.86 KB
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
//contains all functions and structs to render pdf
package main
import (
"fmt"
"io"
"github.com/jung-kurt/gofpdf"
)
func generatePDF(w io.Writer, logs []DisplayLog, data anonStruct) error {
Info.Println("generatePDF()")
//print header
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetAutoPageBreak(true, 20)
pdf.SetMargins(20, 20, 20)
err := printHeader(pdf, data, logs)
if err != nil {
return err
}
//print table
err = printTable(pdf, logs)
if err != nil {
return err
}
err = pdf.Output(w)
if err != nil {
return err
}
return nil
}
func printHeader(pdf *gofpdf.Fpdf, data anonStruct, logs []DisplayLog) error {
err := printUserData(pdf, data)
if err != nil {
return err
}
err = printUserStats(pdf, logs, data)
if err != nil {
return err
}
pdf.Ln(-1)
return pdf.Error()
}
func printUserData(pdf *gofpdf.Fpdf, data anonStruct) error {
pdf.SetFont("Arial", "", 24)
pdf.CellFormat(0, 20, "TimeTrack Report", "", 1, "", false, 0, "")
pdf.SetFont("Arial", "B", 12)
pdf.CellFormat(0, 7, fmt.Sprintf("Name: %s", data.User.Name), "", 1, "", false, 0, "")
pdf.CellFormat(0, 7, fmt.Sprintf("Period: %s - %s", data.From, data.To), "", 1, "", false, 0, "")
return pdf.Error()
}
func printUserStats(pdf *gofpdf.Fpdf, data []DisplayLog, anon anonStruct) error {
stats := calculateStats(data, anon)
pdf.Ln(-1)
pdf.CellFormat(0, 7, fmt.Sprintf("Expected Work time: %s", stats.ExtectedWorkTime), "", 1, "", false, 0, "")
pdf.CellFormat(0, 7, fmt.Sprintf("Actual Work time: %s", stats.ActualWorkTime), "", 1, "", false, 0, "")
pdf.CellFormat(0, 7, fmt.Sprintf("Difference: %s", stats.Delta), "", 1, "", false, 0, "")
pdf.CellFormat(0, 7, fmt.Sprintf("Holidays: %d days", stats.Holidays), "", 1, "", false, 0, "")
pdf.CellFormat(0, 7, fmt.Sprintf("Sick leave: %d days", stats.Sickdays), "", 1, "", false, 0, "")
return pdf.Error()
}
func printTable(pdf *gofpdf.Fpdf, data []DisplayLog) error {
pdf.SetFont("Arial", "B", 12)
pdf.CellFormat(30, 6, "", "B", 0, "", false, 0, "")
pdf.CellFormat(20, 6, "From", "B", 0, "", false, 0, "")
pdf.CellFormat(30, 6, "", "B", 0, "", false, 0, "")
pdf.CellFormat(20, 6, "To", "B", 0, "", false, 0, "")
pdf.CellFormat(30, 6, "Duration", "B", 0, "", false, 0, "")
pdf.CellFormat(20, 6, "Type", "B", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.SetFont("Arial", "", 12)
for _, l := range data {
if l.ToDate == nil {
continue
}
pdf.CellFormat(30, 6, l.DateFrom, "", 0, "", false, 0, "")
pdf.CellFormat(20, 6, l.TimeFrom, "", 0, "", false, 0, "")
if l.Type == workTimeConst {
pdf.CellFormat(30, 6, "", "", 0, "", false, 0, "")
} else {
pdf.CellFormat(30, 6, l.DateTo, "", 0, "", false, 0, "")
}
pdf.CellFormat(20, 6, l.TimeTo, "", 0, "", false, 0, "")
pdf.CellFormat(30, 6, l.Duration, "", 0, "", false, 0, "")
pdf.CellFormat(20, 6, l.Type, "", 0, "", false, 0, "")
pdf.Ln(-1)
}
return pdf.Error()
}