convert.js
6.65 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
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
"use strict";
/** Coordinate container that occupies the whole page **/
const A4 = new Div(
new Vector(0, 0),
new Vector(595, 842)
);
const Pos = {
"beg": 0,
"beg_margin": 5,
"quarter": 25,
"third": 33.33,
"middle": 50,
"two_thirds": 66.67,
"three_quarters": 75,
"end_margin": 95,
"end": 100
};
const containerSize = 86;
function generateDivTree(data) {
/**
* The lambdas serve as "apply" functions
* Each function processes an element, executed in order of appeareance in HTML.
* They all return a coordinate "pointer" to draw.
*/
const functionMapping = [
(pointer, data) => headerToPDF(pointer, data['header']),
(pointer, data) => alineadorToPDF(pointer, data['alineador']),
(pointer, data) => suspensionToPDF(pointer, data['suspension']),
(pointer, data) => frenosToPDF(pointer, data['frenos']),
(pointer, data) => traseroToPDF(pointer, data['trasero']),
(pointer, data) => gaseshumosToPDF(pointer, data['gaseshumos'])
];
/**
* We pass the pointer to each function, which then modify it and passes it to the next,
* We start with (0%,0%)
*/
functionMapping.reduce(
(pointer, treeMaker) => treeMaker(pointer, data),
A4.position
);
return A4;
}
function headerToPDF(pointer, data) {
const header = A4.addChild(pointer, new Vector(Pos.end, 3));
header.addText(
"Dominio: " + data['dominio'],
new Vector(Pos.beg_margin, Pos.two_thirds)
);
header.addText(
"Fecha: " + data['fecha'],
new Vector(Pos.third, Pos.two_thirds)
);
return new Vector(pointer.x, pointer.y + 3);
}
function alineadorToPDF(pointer, data) {
const alineador = A4.addChild(pointer, new Vector(Pos.end, 5));
alineador.addText(
"Resultado Pruebas en Banco de Alineador al Paso",
new Vector(Pos.beg_margin + 2, Pos.third)
);
alineador.addText(
"Eje Delantero",
new Vector(Pos.beg_margin + 2, Pos.two_thirds)
);
alineador.addText(
data['eje_delan'],
new Vector(Pos.quarter, Pos.two_thirds)
);
return new Vector(pointer.x, pointer.y + 5);
}
function suspensionToPDF(pointer, data) {
const suspension = A4.addChild(pointer, new Vector(Pos.end, 15));
const divsize = new Vector(containerSize, Pos.middle);
const eje1 = suspension.addChild(new Vector(Pos.beg_margin + 2, Pos.beg), divsize)
const eje2 = suspension.addChild(new Vector(Pos.beg_margin + 2, Pos.middle), divsize)
const table = (container, eje, RI, peso, RD) => {
container.addTexts([
[`Resultado Pruebas en Banco de Suspensión - Eje ${eje}`, new Vector(0, Pos.third)],
["Rendimiento Izquierdo", new Vector(Pos.beg, Pos.two_thirds)],
[RI, new Vector(Pos.beg + 10, Pos.end)],
["Peso Total del Eje", new Vector(Pos.middle - 10, Pos.two_thirds)],
[peso, new Vector(Pos.middle - 3, Pos.end)],
["Rendimiento Derecho", new Vector(Pos.three_quarters - 5, Pos.two_thirds)],
[RD, new Vector(Pos.three_quarters + 5, Pos.end)]
]);
};
table(eje1, 1, data[0]["ren_izq"], data[0]["peso"], data[0]["ren_der"])
table(eje2, 2, data[1]["ren_izq"], data[1]["peso"], data[1]["ren_der"])
return new Vector(pointer.x, pointer.y + 15);
}
function frenosToPDF(pointer, data) {
const frenos = A4.addChild(pointer, new Vector(Pos.end, 60));
const divsize = new Vector(containerSize, Pos.quarter);
const eje1 = frenos.addChild(new Vector(Pos.beg_margin + 2, Pos.beg), divsize);
const eje2 = frenos.addChild(new Vector(Pos.beg_margin + 2, Pos.quarter), divsize);
const eje3 = frenos.addChild(new Vector(Pos.beg_margin + 2, Pos.middle), divsize);
const eje4 = frenos.addChild(new Vector(Pos.beg_margin + 2, Pos.three_quarters), divsize);
const table = (container, eje, FI, RI, OI, peso, FD, RD, OD) => {
container.addTexts([
[`Diagnóstico de Frenos - Eje ${eje}`, new Vector(Pos.beg, Pos.beg_margin + 10)],
["Fuerza IZ", new Vector(Pos.beg_margin, Pos.quarter)],
[FI, new Vector(Pos.beg_margin + 3, Pos.quarter + 10)],
["Res.Rodadura IZ", new Vector(Pos.beg_margin, Pos.middle)],
[RI, new Vector(Pos.beg_margin + 3, Pos.middle + 10)],
["Ovalidad IZ", new Vector(Pos.beg_margin, Pos.three_quarters)],
[OI, new Vector(Pos.beg_margin + 3, Pos.three_quarters + 10)],
["Peso del Eje", new Vector(Pos.middle - 10, Pos.quarter)],
[peso, new Vector(Pos.middle - 5, Pos.quarter + 10)],
["Fuerza DE", new Vector(Pos.three_quarters, Pos.quarter)],
[FD, new Vector(Pos.three_quarters + 3, Pos.quarter + 10)],
["Res.Rodadura DE", new Vector(Pos.three_quarters, Pos.middle)],
[RD, new Vector(Pos.three_quarters + 3, Pos.middle + 10)],
["Ovalidad DE", new Vector(Pos.three_quarters, Pos.three_quarters)],
[OD, new Vector(Pos.three_quarters + 3, Pos.three_quarters + 10)]
]);
};
let x;
x = data[0];
table(eje1, 1, x["f_izq"], x["res_izq"], x["ov_izq"], x["peso"], x["f_der"], x["res_der"], x["ov_der"])
x = data[1];
table(eje2, 2, x["f_izq"], x["res_izq"], x["ov_izq"], x["peso"], x["f_der"], x["res_der"], x["ov_der"])
x = data[2];
table(eje3, 3, x["f_izq"], x["res_izq"], x["ov_izq"], x["peso"], x["f_der"], x["res_der"], x["ov_der"])
x = data[3];
table(eje4, 4, x["f_izq"], x["res_izq"], x["ov_izq"], x["peso"], x["f_der"], x["res_der"], x["ov_der"])
return new Vector(pointer.x, pointer.y + 60);
}
function traseroToPDF(pointer, data) {
const trasero = A4.addChild(new Vector(pointer.x + Pos.beg_margin + 2, pointer.y), new Vector(containerSize, 9));
trasero.addTexts([
["Diagnóstico de Frenos - Eje Trasero - Frenos de Mano", new Vector(Pos.beg, Pos.third)],
["Fuerza IZ", new Vector(Pos.beg_margin, Pos.two_thirds)],
[data['f_izq'], new Vector(Pos.beg_margin, Pos.end)],
["Fuerza DE", new Vector(Pos.three_quarters, Pos.two_thirds)],
[data['f_der'], new Vector(Pos.three_quarters, Pos.end)],
]);
return new Vector(pointer.x, pointer.y + 9);
}
function gaseshumosToPDF(pointer, data) {
const gaseshumos = A4.addChild(new Vector(pointer.x + Pos.beg_margin + 2, pointer.y), new Vector(containerSize, 9));
gaseshumos.addTexts([
["Resultado Analizador de Gases", new Vector(Pos.beg, Pos.third)],
["Resultado Analizador de Humos", new Vector(Pos.two_thirds, Pos.third)],
[`CO ${data['co']} % HC ${data['hc']} ppm`, new Vector(Pos.beg, Pos.two_thirds)],
[`Medición ${data['med']}`, new Vector(Pos.three_quarters, Pos.two_thirds)]
]);
return new Vector(pointer.x, pointer.y + 9);
}
function compile(tree, pdf) {
pdf.setFont("Courier");
pdf.setFontType("normal");
pdf.setFontSize(10);
tree.children.forEach( child => {
switch (child.constructor.name) {
case "Text":
pdf.text(child.text, child.position.x, child.position.y);
break;
case "Div":
if (child.drawOutline) {
child.drawDivBox(pdf);
}
compile(child, pdf);
break;
}
});
return pdf;
}