convert.js
3.91 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
"use strict";
/** Coordinate container that occupies the whole page **/
const A4 = new Div(
new Vector(0, 0),
new Vector(595, 842)
);
const Position = {
"beggining": 0,
"beggining_margin": 5,
"quarter": 25,
"third": 33.33,
"middle": 50,
"two_thirds": 66.67,
"three_quarters": 75,
"end_margin": 95,
"end": 100
};
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(Position.end, 3));
header.addText(
"Dominio: " + data['dominio'],
new Vector(Position.beggining_margin, Position.two_thirds)
);
header.addText(
"Fecha: " + data['fecha'],
new Vector(Position.third, Position.two_thirds)
);
return new Vector(pointer.x, pointer.y + 3);
}
function alineadorToPDF(pointer, data) {
const alineador = A4.addChild(pointer, new Vector(Position.end, 5));
const title = alineador.addText(
"Resultado Pruebas en Banco de Alineador al Paso",
new Vector(Position.beggining_margin + 2, Position.third)
);
const subtitle = alineador.addText(
"Eje Delantero",
new Vector(Position.beggining_margin + 2, Position.two_thirds)
);
const value = alineador.addText(
data['eje_delan'],
new Vector(Position.quarter, Position.two_thirds)
);
return new Vector(pointer.x, pointer.y + 5);
}
function suspensionToPDF(pointer, data) {
const suspension = A4.addChild(pointer, new Vector(Position.end, 15));
const eje1 = suspension.addChild(new Vector(Position.beggining_margin + 2, 0), new Vector(86, 50))
const eje2 = suspension.addChild(new Vector(Position.beggining_margin + 2, 50), new Vector(86, 50))
const table = (container, eje, RI, peso, RD) => {
container.addText(
`Resultado Pruebas en Banco de Suspensión - Eje ${eje}`,
new Vector(0, Position.third)
);
container.addText(
"Rendimiento Izquierdo",
new Vector(0, Position.two_thirds)
);
container.addText(
RI,
new Vector(0, Position.end)
);
container.addText(
"Peso Total del Eje",
new Vector(37, Position.two_thirds)
);
container.addText(
peso,
new Vector(45, Position.end)
);
container.addText(
"Rendimiento Derecho",
new Vector(80, Position.two_thirds)
);
container.addText(
RD,
new Vector(90, Position.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) {
A4.addChild("frenos", pointer, new Vector(Position.end, 60));
return new Vector(pointer.x, pointer.y + 60);
}
function traseroToPDF(pointer, data) {
A4.addChild("trasero", pointer, new Vector(Position.end, 9));
return new Vector(pointer.x, pointer.y + 9);
}
function gaseshumosToPDF(pointer, data) {
A4.addChild("gaseshumos", pointer, new Vector(Position.end, 9));
return new Vector(pointer.x, pointer.y + 9);
}
function compile(tree, pdf) {
tree.children.forEach( child => {
switch (child.constructor.name) {
case "Text":
pdf.text(child.text, child.position.x, child.position.y);
break;
case "Div":
compile(child, pdf);
break;
}
});
}