convert.js 6.65 KB
"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;
}