-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateAndSavePDF.go
239 lines (205 loc) · 7.76 KB
/
generateAndSavePDF.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
package main
import (
"fmt"
"sync"
"github.com/jung-kurt/gofpdf"
"gonum.org/v1/plot"
"gonum.org/v1/plot/vg"
)
func generateAndSavePDF(rollPlot, pitchPlot, yawPlot, altitudePlot, errorRollPlot, errorPitchPlot, errorYawPlot, errorAltitudePlot *plot.Plot,
rollRMS, pitchRMS, yawRMS, altitudeRMS, rollSSE, pitchSSE, yawSSE, altitudeSSE string) error {
// Creating new PDF
pdf := gofpdf.New("P", "mm", "A4", "")
// New page (first)
pdf.AddPage()
// Title for page
pdf.SetFont("times", "B", 14)
pdf.Cell(40, 10, "Filght data to PDF raport microservice - control quality indicators")
pdf.Ln(10)
// Adding variables to PDF
name := "Sebastian Brzustowicz"
email := "Se.Brzustowicz@gmail.com"
additionalInfo := "This microservice is distributed under MIT licencse."
pdf.SetFont("times", "B", 10)
pdf.Cell(0, 10, fmt.Sprintf("Author: %s", name))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("E-mail: %s", email))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("Additional info: %s", additionalInfo))
pdf.SetFont("times", "", 10)
pdf.Ln(10)
pdf.Cell(0, 10, fmt.Sprintf("In order to present the results of the control method RMS indicator was applied, which is the root mean square error."))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("This indicator shows what the spread of values around the mean error value is."))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("The RMS criterion for each rotary axes is defined as follows:"))
pdf.Ln(10)
pdf.Cell(0, 10, fmt.Sprintf("J_RMS = sqrt(sum((e_n-m)^2)/N)"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("where"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("J_RMS - RMS quality indicator"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("e - actual error"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("m - mean error"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("N - number of samples"))
pdf.Ln(10)
// Headers setting for table
pdf.SetFont("times", "B", 12)
pdf.SetFillColor(200, 220, 255)
// First row
pdf.CellFormat(40, 10, "Measured variable", "1", 0, "", true, 0, "")
pdf.CellFormat(100, 10, "RMS indicator", "1", 0, "", true, 0, "")
// End of row
pdf.Ln(-1)
// Table style
pdf.SetFont("times", "", 12)
pdf.SetFillColor(255, 255, 255)
// Table data
pdf.CellFormat(40, 10, fmt.Sprintf("Roll"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(rollRMS), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Pitch"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(pitchRMS), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Yaw"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(yawRMS), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Altitude"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(altitudeRMS), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.Ln(5)
pdf.SetFont("times", "", 10)
pdf.Cell(0, 10, fmt.Sprintf("The second indicator is the sum of squared errors."))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("Sum of squared errors for each rotary axes is defined as follows:"))
pdf.Ln(10)
pdf.Cell(0, 10, fmt.Sprintf("J_SSE = sum((e_n)^2))"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("where"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("J_SSE - quality indicator"))
pdf.Ln(5)
pdf.Cell(0, 10, fmt.Sprintf("e - actual error"))
pdf.Ln(10)
// Headers setting for table
pdf.SetFont("times", "B", 12)
pdf.SetFillColor(200, 220, 255)
// First row
pdf.CellFormat(40, 10, "Measured variable", "1", 0, "", true, 0, "")
pdf.CellFormat(100, 10, "SSE indicator", "1", 0, "", true, 0, "")
// End of row
pdf.Ln(-1)
// Table style
pdf.SetFont("times", "", 12)
pdf.SetFillColor(255, 255, 255)
// Table data
pdf.CellFormat(40, 10, fmt.Sprintf("Roll"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(rollSSE), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Pitch"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(pitchSSE), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Yaw"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(yawSSE), "1", 0, "", false, 0, "")
pdf.Ln(-1)
pdf.CellFormat(40, 10, fmt.Sprintf("Altitude"), "1", 0, "", false, 0, "")
pdf.CellFormat(100, 10, fmt.Sprintf(altitudeSSE), "1", 0, "", false, 0, "")
pdf.Ln(-1)
// New page (second)
pdf.AddPage()
// Title for page
pdf.SetFont("times", "B", 14)
pdf.Cell(40, 10, "Filght data to PDF raport microservice - control visualisation")
pdf.SetFont("times", "B", 10)
pdf.Ln(10)
// Goroutines
var wg sync.WaitGroup
wg.Add(4)
pngRollPlot := "rollPlot.png"
go savePlotAsync(rollPlot, pngRollPlot, &wg)
pngPitchPlot := "pitchPlot.png"
go savePlotAsync(pitchPlot, pngPitchPlot, &wg)
pngYawPlot := "yawPlot.png"
go savePlotAsync(yawPlot, pngYawPlot, &wg)
pngAltitudePlot := "altitudePlot.png"
go savePlotAsync(altitudePlot, pngAltitudePlot, &wg)
wg.Wait()
pdf.Ln(10)
pdf.Image(pngRollPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetRoll := 85
pdf.SetX(pdf.GetX() + float64(xOffsetRoll))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 1.1: Roll data"))
pdf.Ln(12)
pdf.Image(pngPitchPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetPitch := 85
pdf.SetX(pdf.GetX() + float64(xOffsetPitch))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 1.2: Pitch data"))
pdf.Ln(12)
pdf.Image(pngYawPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetYaw := 85
pdf.SetX(pdf.GetX() + float64(xOffsetYaw))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 1.3: Yaw data"))
pdf.Ln(12)
pdf.Image(pngAltitudePlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetAltitude := 82
pdf.SetX(pdf.GetX() + float64(xOffsetAltitude))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 1.4: Altitude data"))
// New page (third)
pdf.AddPage()
// Title for page
pdf.SetFont("times", "B", 14)
pdf.Cell(40, 10, "Filght data to PDF raport microservice - control errors")
pdf.SetFont("times", "B", 10)
pdf.Ln(10)
wg.Add(4)
pngRollErrorPlot := "rollErrorPlot.png"
go saveErrorPlotAsync(errorRollPlot, pngRollErrorPlot, &wg)
pngPitchErrorPlot := "pitchErrorPlot.png"
go saveErrorPlotAsync(errorPitchPlot, pngPitchErrorPlot, &wg)
pngYawErrorPlot := "yawErrorPlot.png"
go saveErrorPlotAsync(errorYawPlot, pngYawErrorPlot, &wg)
pngAltitudeErrorPlot := "altitudeErrorPlot.png"
go saveErrorPlotAsync(errorAltitudePlot, pngAltitudeErrorPlot, &wg)
wg.Wait()
pdf.Ln(10)
pdf.Image(pngRollErrorPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetRollError := 82
pdf.SetX(pdf.GetX() + float64(xOffsetRollError))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 2.1: Roll error data"))
pdf.Ln(12)
pdf.Image(pngPitchErrorPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetPitchError := 82
pdf.SetX(pdf.GetX() + float64(xOffsetPitchError))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 2.2: Pitch error data"))
pdf.Ln(12)
pdf.Image(pngYawErrorPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetYawError := 82
pdf.SetX(pdf.GetX() + float64(xOffsetYawError))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 2.3: Yaw error data"))
pdf.Ln(12)
pdf.Image(pngAltitudeErrorPlot, 10, pdf.GetY(), 190, 50, true, "", 0, "")
xOffsetAltitudeError := 79
pdf.SetX(pdf.GetX() + float64(xOffsetAltitudeError))
pdf.Cell(210, 10, fmt.Sprintf("Fig. 2.4: Altitude error data"))
// Saving pdf to file
err := pdf.OutputFileAndClose("Data-report.pdf")
if err != nil {
return fmt.Errorf("Error when saving file to PDF: %v", err)
}
return nil
}
func savePlotAsync(plot *plot.Plot, filename string, wg *sync.WaitGroup) {
defer wg.Done()
if err := plot.Save(10*vg.Inch, 3*vg.Inch, filename); err != nil {
fmt.Printf("Error when saving plot to PNG (%s): %v\n", filename, err)
}
}
func saveErrorPlotAsync(errorPlot *plot.Plot, filename string, wg *sync.WaitGroup) {
defer wg.Done()
if err := errorPlot.Save(10*vg.Inch, 3*vg.Inch, filename); err != nil {
fmt.Printf("Error when saving error plot to PNG (%s): %v\n", filename, err)
}
}