pdf1.js 6.24 KB
class PDF1 {

	"use strict";

	
	constructor() {
		/** Coordinate container that occupies the whole page **/
		this.A4 = new Div(
			new Vector(0, 0),
			new Vector(595, 842)
		);
	
		this.containerSize = 86;
	}


	pdf(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 pdfthis = this;
		const functionMapping = [
			(pointer, data) => pdfthis.headerToPDF(pointer, data['header']),
			(pointer, data) => pdfthis.alineadorToPDF(pointer, data['alineador']),
			(pointer, data) => pdfthis.suspensionToPDF(pointer, data['suspension']),
			(pointer, data) => pdfthis.frenosToPDF(pointer, data['frenos']),
			(pointer, data) => pdfthis.traseroToPDF(pointer, data['trasero']),
			(pointer, data) => pdfthis.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),
			pdfthis.A4.position
		);

		return this.A4;
	}

	headerToPDF(pointer, data) {

		const header = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 3)));

		header.addRelativeChildren([
			new Text("Dominio: " + data['dominio'], new Vector(Pos.beg_margin, Pos.two_thirds)),

			new Text("Fecha: " + data['fecha'], new Vector(Pos.third, Pos.two_thirds))
		]);

		return pointer.addxy(0,3);
	}

	alineadorToPDF(pointer, data) {

		const alineador = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 5)));

		alineador.addRelativeChildren([
			new Text("Resultado Pruebas en Banco de Alineador al Paso", new Vector(Pos.beg_margin + 2, Pos.third)),

			new Text("Eje Delantero", new Vector(Pos.beg_margin + 2, Pos.two_thirds)),

			new Text(data['eje_delan'], new Vector(Pos.quarter, Pos.two_thirds))
		]);

		return pointer.addxy(0, 5);
	}

	suspensionToPDF(pointer, data) {

		const suspension = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 15)));

		const divsize = new Vector(this.containerSize, Pos.middle);

		const ejes = suspension.addRelativeChildren([
			new Div(new Vector(Pos.beg_margin + 2, Pos.beg), divsize),
			new Div(new Vector(Pos.beg_margin + 2, Pos.middle), divsize)
		]);

		const table = (container, eje, RI, peso, RD) => {
			
			container.addRelativeChildren([
				new Text(`Resultado Pruebas en Banco de Suspensión - Eje ${eje}`, new Vector(0, Pos.third)),
			
				new Text("Rendimiento Izquierdo", new Vector(Pos.beg, Pos.two_thirds)),

				new Text(RI, new Vector(Pos.beg + 10, Pos.end)),
			
				new Text("Peso Total del Eje", new Vector(Pos.middle - 10, Pos.two_thirds)),

				new Text(peso, new Vector(Pos.middle - 3, Pos.end)),
			
				new Text("Rendimiento Derecho", new Vector(Pos.three_quarters - 5, Pos.two_thirds)),

				new Text(RD, new Vector(Pos.three_quarters + 5, Pos.end))
			]);
		};

		for (let i = 0; i < ejes.length; i++) {
			const x = data[i];
			table(ejes[i], i-1, x["ren_izq"], x["peso"], x["ren_der"])		
		}

		return pointer.addxy(0, 15);
	}

	frenosToPDF(pointer, data) {
		
		const frenos = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 60)));

		const divsize = new Vector(this.containerSize, Pos.quarter);

		const ejes = frenos.addRelativeChildren([
			new Div(new Vector(Pos.beg_margin + 2, Pos.beg), divsize),
			new Div(new Vector(Pos.beg_margin + 2, Pos.quarter), divsize),
			new Div(new Vector(Pos.beg_margin + 2, Pos.middle), divsize),
			new Div(new Vector(Pos.beg_margin + 2, Pos.three_quarters), divsize)
		]);

		const table = (container, eje, FI, RI, OI, peso, FD, RD, OD) => {

			container.addRelativeChildren([
				new Text(`Diagnóstico de Frenos - Eje ${eje}`, new Vector(Pos.beg, Pos.beg_margin + 10)),

				new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.quarter)),
				
				new Text(FI, new Vector(Pos.beg_margin + 3, Pos.quarter + 10)),
				
				new Text("Res.Rodadura IZ", new Vector(Pos.beg_margin, Pos.middle)),
				
				new Text(RI, new Vector(Pos.beg_margin + 3, Pos.middle + 10)),
				
				new Text("Ovalidad IZ", new Vector(Pos.beg_margin, Pos.three_quarters)),
				
				new Text(OI, new Vector(Pos.beg_margin + 3, Pos.three_quarters + 10)),
				
				new Text("Peso del Eje", new Vector(Pos.middle - 10, Pos.quarter)),
				
				new Text(peso, new Vector(Pos.middle - 5, Pos.quarter + 10)),

				new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.quarter)),

				new Text(FD, new Vector(Pos.three_quarters + 3, Pos.quarter + 10)),

				new Text("Res.Rodadura DE", new Vector(Pos.three_quarters, Pos.middle)),

				new Text(RD, new Vector(Pos.three_quarters + 3, Pos.middle + 10)),

				new Text("Ovalidad DE", new Vector(Pos.three_quarters, Pos.three_quarters)),

				new Text(OD, new Vector(Pos.three_quarters + 3, Pos.three_quarters + 10))

			]);
		};

		for (let i = 0; i < ejes.length; i++) {
			const x = data[i];
			table(ejes[i], i-1, x["f_izq"], x["res_izq"], x["ov_izq"], x["peso"], x["f_der"], x["res_der"], x["ov_der"]);
		}

		return pointer.addxy(0, 60);
	}

	traseroToPDF(pointer, data) {
		
		const trasero = this.A4.addRelativeChild(new Div(pointer.addxy(Pos.beg_margin + 2, 0), new Vector(this.containerSize, 9)));

		trasero.addRelativeChildren([

			new Text("Diagnóstico de Frenos - Eje Trasero - Frenos de Mano", new Vector(Pos.beg, Pos.third)),
 
			new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.two_thirds)),

			new Text(data['f_izq'], new Vector(Pos.beg_margin, Pos.end)),

			new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.two_thirds)),

			new Text(data['f_der'], new Vector(Pos.three_quarters, Pos.end)),

		]);

		return pointer.addxy(0, 9);
	}

	gaseshumosToPDF(pointer, data) {
		
		const gaseshumos = this.A4.addRelativeChild(new Div(pointer.addxy(Pos.beg_margin + 2, 0), new Vector(this.containerSize, 9)));
		
		gaseshumos.addRelativeChildren([
			
			new Text("Resultado Analizador de Gases", new Vector(Pos.beg, Pos.third)),
			
			new Text("Resultado Analizador de Humos", new Vector(Pos.two_thirds, Pos.third)),
			
			new Text(`CO    ${data['co']} %        HC    ${data['hc']} ppm`, new Vector(Pos.beg, Pos.two_thirds)),
			
			new Text(`Medición    ${data['med']}`, new Vector(Pos.three_quarters, Pos.two_thirds))
			
		]);

		return pointer.addxy(0, 9);
	}
}