convert.js
6.31 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
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
}
/** Coordinate container that occupies the whole page **/
const A4 = new Div(
new Vector(0,0),
new Vector(210,297)
);
function divToPDF(div) {
let pdf = new jsPDF('portrait', 'mm', 'a4');
pdf.setFont("courier", "normal");
pdf.setFontSize(10);
/**
* 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.
*/
functionMapping = [
(pointer, div, pdf) => headerToPDF(pointer, div, pdf),
(pointer, div, pdf) => alineadorToPDF(pointer, div, pdf),
(pointer, div, pdf) => suspensionToPDF(pointer, div, pdf),
(pointer, div, pdf) => frenosToPDF(pointer, div, pdf),
(pointer, div, pdf) => traseroToPDF(pointer, div, pdf),
(pointer, div, pdf) => gaseshumosToPDF(pointer, div, pdf),
];
/**
* We pass the pointer to each function, which then modify it and passes it to the next,
* We start with (20,20)
* f_it is a function iterator
*/
Array.from(div.children).reduce(
(pointer, child) => {
if (child.nodeName == "DIV") {
n_pointer = functionMapping[ pointer.f_it ](
/** Remove the function index **/
new Vector(pointer.x, pointer.y),
child,
pdf
)
/** Add it and add 1 to it **/
n_pointer.f_it = pointer.f_it + 1;
return n_pointer;
}
return pointer;
},
{"x" : 0, "y" : 0, "f_it" : 0}
);
window.open(pdf.output('bloburl'));
}
function headerToPDF(pointer, div, pdf) {
/** Header goes from pointer to 100% width and 5% heigth **/
header = A4.addChild("header", pointer, new Vector(Position.end, 3));
/** First part of the text goes 5% to the right and in the middle vertically. no size provided **/
dom = header.addChild("dominio", new Vector(Position.beggining_margin, Position.two_thirds), new Vector(0,0))
/** Second part of the text goes a third to the right and in the middle vertically. no size provided **/
fecha = header.addChild("fecha", new Vector(Position.third, Position.two_thirds), new Vector(0,0))
/** Get the first row that you find **/
tr = getDescendantByTag(div, "tr");
pdf.text(
tr.children[0].innerText,
dom.position.x,
dom.position.y,
{"align" : "left"}
)
pdf.text(
tr.children[1].innerText,
fecha.position.x,
fecha.position.y,
{"align" : "left"}
)
//header.drawDivBox(pdf);
/** Next safe position to draw (in percentage). same x, next available y **/
return new Vector(pointer.x, pointer.y + 3);
}
function alineadorToPDF(pointer, div, pdf) {
/** Alineador goes from pointer to 100% width and 10% heigth **/
A4.addChild("alineador", pointer, new Vector(Position.end, 7));
/** Title goes to the left and up **/
title = A4.children.alineador.addChild("title", new Vector(Position.beggining_margin + 2, Position.third), new Vector(0,0))
/** Subtitle goes left and down **/
subtitle = A4.children.alineador.addChild("subtitle", new Vector(Position.beggining_margin + 2, Position.two_thirds), new Vector(0,0))
/** Value goes a little to the right and down **/
value = A4.children.alineador.addChild("value", new Vector(Position.quarter, Position.two_thirds), new Vector(0,0))
pdf.text(
"Resultado Pruebas de Alineador al Paso",
title.position.x,
title.position.y,
{"align" : "left"}
)
pdf.text(
"Eje Delantero",
subtitle.position.x,
subtitle.position.y,
{"align" : "left"}
)
n = div.innerText // take the whole text
.replace(/[\n]/g, "") // remove the new lines
.split(" ") // make array of words
.pop() // take the last one
pdf.text(
n,
value.position.x,
value.position.y,
{"align" : "left"}
)
//A4.children.alineador.drawDivBox(pdf);
return new Vector(pointer.x, pointer.y + 7);
}
function suspensionToPDF(pointer, div, pdf) {
suspension = A4.addChild("suspension", pointer, new Vector(100, 15));
eje1 = suspension.addChild("eje1", new Vector(Position.beggining_margin + 2, 0), new Vector(90,50))
eje2 = suspension.addChild("eje2", new Vector(Position.beggining_margin + 2, 50), new Vector(90,50))
table = (container, pdf, eje, RI, p, RD) => {
title = container.addChild("title", new Vector(0, Position.third), new Vector(0,0))
ri = container.addChild("ri", new Vector(0, Position.two_thirds), new Vector(0,0))
ri_value = container.addChild("ri_value", new Vector(10, Position.end), new Vector(0,0))
peso = container.addChild("peso", new Vector(37, Position.two_thirds), new Vector(0,0))
peso_value = container.addChild("peso_value", new Vector(45, Position.end), new Vector(0,0))
rd = container.addChild("rd", new Vector(Position.three_quarters - 7, Position.two_thirds), new Vector(0,0))
rd_value = container.addChild("rd_value", new Vector(Position.three_quarters, Position.end), new Vector(0,0))
pdf.text(
`Resultado Pruebas en Banco de Suspensión - Eje ${eje}`,
title.position.x,
title.position.y,
{"align" : "left"}
);
pdf.text(
`Rendimiento Izquierdo`,
ri.position.x,
ri.position.y,
{"align" : "left"}
);
pdf.text(
RI,
ri_value.position.x,
ri_value.position.y,
{"align" : "left"}
);
pdf.text(
`Peso Total del Eje`,
peso.position.x,
peso.position.y,
{"align" : "left"}
);
pdf.text(
p,
peso_value.position.x,
peso_value.position.y,
{"align" : "left"}
);
pdf.text(
`Rendimiento Derecho`,
rd.position.x,
rd.position.y,
{"align" : "left"}
);
pdf.text(
RD,
rd_value.position.x,
rd_value.position.y,
{"align" : "left"}
);
};
table(eje1, pdf, 1, "0.000", "0.000", "0.000")
table(eje2, pdf, 2, "0.000", "0.000", "0.000")
//A4.children.suspension.drawDivBox(pdf);
return new Vector(pointer.x, pointer.y + 15);
}
function frenosToPDF(pointer, div, pdf) {
A4.addChild("frenos", pointer, new Vector(Position.end, 57));
//A4.children.frenos.drawDivBox(pdf);
return new Vector(pointer.x, pointer.y + 57);
}
function traseroToPDF(pointer, div, pdf) {
A4.addChild("trasero", pointer, new Vector(Position.end, 9));
//A4.children.trasero.drawDivBox(pdf);
return new Vector(pointer.x, pointer.y + 9);
}
function gaseshumosToPDF(pointer, div, pdf) {
A4.addChild("gaseshumos", pointer, new Vector(Position.end, 9));
//A4.children.gaseshumos.drawDivBox(pdf);
return new Vector(pointer.x, pointer.y + 9);
}