-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.go
125 lines (105 loc) · 3.27 KB
/
pdf.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
package json2docs
import (
"encoding/json"
"github.com/buger/jsonparser"
"github.com/jung-kurt/gofpdf"
)
func PdfTabularGenerator(format []byte, data []byte, filename string) (string, error) {
// Parse the format and data JSON arrays
var formatData Format
err := json.Unmarshal(format, &formatData)
if err != nil {
return "", err
}
// Create a new PDF document
pdf := gofpdf.New("P", "mm", "A4", "")
// Add a new page
pdf.AddPage()
// Set font and font size for the header
pdf.SetFont("Arial", "B", 16)
// Print header lines
for _, header := range formatData.Header {
pdf.SetXY(10, float64(header.Line*10))
pdf.CellFormat(0, 10, header.Text, "", 0, "C", false, 0, "")
}
// Set font and font size for the table
pdf.SetFont("Arial", "", 12)
// Set table header background color
pdf.SetFillColor(240, 240, 240)
// Calculate total table width
totalWidth := 0
for _, header := range formatData.BodyHeader {
totalWidth += header.Width
}
for _, field := range formatData.BodyFields {
totalWidth += field.Width
}
// Calculate available width on the page
pageWidth, _ := pdf.GetPageSize()
leftMargin, _, rightMargin, _ := pdf.GetMargins()
availableWidth := pageWidth - leftMargin - rightMargin
// Adjust table width to fit the page
if totalWidth > int(availableWidth) {
scaleFactor := float64(availableWidth) / float64(totalWidth)
for i := range formatData.BodyHeader {
formatData.BodyHeader[i].Width = int(float64(formatData.BodyHeader[i].Width) * scaleFactor)
}
for i := range formatData.BodyFields {
formatData.BodyFields[i].Width = int(float64(formatData.BodyFields[i].Width) * scaleFactor)
}
}
tableCellWidth := availableWidth / float64(len(formatData.BodyFields))
// Print table header
x := 10.0
y := float64(len(formatData.Header)*10 + 20)
for _, header := range formatData.BodyHeader {
pdf.Rect(x, y, float64(tableCellWidth), 10, "F")
pdf.SetXY(x, y)
pdf.CellFormat(float64(tableCellWidth), 10, header.Text, "", 0, "C", false, 0, "")
x += float64(tableCellWidth)
}
// Set body fields values
var items [][]byte
i := 0
jsonparser.ArrayEach(data, func(item []byte, dataType jsonparser.ValueType, offset int, err error) {
y += 10
items = append(items, item)
jsonparser.ObjectEach(item, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
x = 10
for _, field := range formatData.BodyFields {
if field.Field == string(key) {
pdf.Rect(x, y, float64(tableCellWidth), 10, "")
pdf.SetXY(x, y)
txt := string(value)
if IsDigit(txt) {
pdf.CellFormat(float64(tableCellWidth), 10, txt, "", 0, "R", false, 0, "")
} else {
pdf.CellFormat(float64(tableCellWidth), 10, txt, "", 0, "C", false, 0, "")
}
}
x += float64(tableCellWidth)
}
return nil
})
i++
})
// Print summary
x = 10.0
for _, summary := range formatData.Summary {
pdf.Rect(x, y, tableCellWidth, 10, "F")
pdf.SetXY(x, y)
if IsDigit(summary.Text) {
pdf.CellFormat(tableCellWidth, 10, summary.Text, "", 0, "R", false, 0, "")
} else {
pdf.CellFormat(tableCellWidth, 10, summary.Text, "", 0, "C", false, 0, "")
}
x += tableCellWidth
}
// Save the PDF file
filename = filename + ".pdf"
err = pdf.OutputFileAndClose(filename)
if err != nil {
return "", err
}
return filename, nil
}