pdf2.js 11 KB
class PDF2 {

	"use strict";


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

		this.containerSize = 86;
		this.font = {
			"font": "courier",
			"type": "normal",
			"size": 10
		};
		this.Dominio = ""
	}


	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;
		console.log(data)
		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']),
			(pointer, data) => pdfthis.ruidoToPDF(pointer, data['ruido'])
		];

		/**
		 * 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;
	}
	todosValoresIguales(obj) {
		// Obtener todos los valores del objeto en un array
		const valores = Object.values(obj);
		
		// Verificar si todos los valores son iguales
		for (let i = 1; i < valores.length; i++) {
		  if (valores[i] !== valores[0]) {
			return true; // Al menos un valor es diferente
		  }
		}
		
		return false; // Todos los valores son iguales
	  }

	headerToPDF(pointer, data) {

		//const header = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 5)));
		this.Dominio = data['patente'].toUpperCase()
		//header.addRelativeChildren([
		//	new Text("Dominio: " + data['patente'], new Vector(Pos.beg_margin , Pos.third), this.font),
		//	new Text("Ingeniero: " + data['ingeniero'], new Vector(Pos.quarter + 4, Pos.third), this.font),
		//	new Text("Fecha y hora " + data['fecha'] + " " + data['hora'], new Vector(Pos.third + 30, Pos.third), this.font)
		//]);

		//return pointer.addxy(0, 3);
		return pointer
	}

	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), this.font),

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

			//new Text(data['eje_delantero'], new Vector(Pos.quarter, Pos.two_thirds), this.font)
		//]);

		//return pointer.addxy(0, 5);
		return pointer
	}

	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(`${this.Dominio} - Resultado Pruebas en Banco de Suspensión - Eje ${eje}`, new Vector(0, Pos.third, ), this.font),

				new Text("Rendimiento Izquierdo", new Vector(Pos.beg, Pos.two_thirds), this.font),

				new Text(RI, new Vector(Pos.beg + 10, Pos.end, ), this.font),

				new Text("Peso Total del Eje", new Vector(Pos.middle - 10, Pos.two_thirds), this.font),

				new Text(peso, new Vector(Pos.middle - 3, Pos.end), this.font),

				new Text("Rendimiento Derecho", new Vector(Pos.three_quarters - 5, Pos.two_thirds), this.font),

				new Text(RD, new Vector(Pos.three_quarters + 5, Pos.end), this.font)
			]);
		};
		let contador = 0
		for (let i = 0; i < 2; i++) {
			if (data[`peso_estatico_${i + 1}`] != '---') {
				contador = 15
				table(
					ejes[i],
					i + 1,
					data[`rendimiento_izquierdo_${i+1}`],
					data[`peso_estatico_${i+1}`],
					data[`rendimiento_derecho_${i+1}`]
				);
			}
		}

		return pointer.addxy(0, contador);
	}

	frenosToPDF(pointer, data) {

		const frenos = this.A4.addRelativeChild(new Div(pointer, new Vector(Pos.end, 60))); //Esto hay q achicar

		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, DFT,Pi,Pd) => {

			if (FI === RI === OI === peso === FD === RD === OD) {
				console.log(eje)
			} else {

				container.addRelativeChildren([
					new Text(`${this.Dominio} - Diagnóstico de Frenos - Eje ${eje}`, new Vector(Pos.beg, Pos.beg_margin + 10), this.font),
					//new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.quarter), this.font),
					//new Text(`${FI} KN`, new Vector(Pos.beg_margin + 3, Pos.quarter + 10), this.font),
					new Text("Peso Lado Izquierdo", new Vector(Pos.beg_margin, Pos.quarter), this.font),
					new Text(`${Pi} Kg`, new Vector(Pos.beg_margin + 3, Pos.quarter + 10), this.font),
					new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.middle), this.font),
					new Text(`${FI} KN`, new Vector(Pos.beg_margin + 3, Pos.middle + 10), this.font),
					//new Text("Res.Rodadura IZ", new Vector(Pos.beg_margin, Pos.middle), this.font),
					//new Text(`${RI} Kg`, new Vector(Pos.beg_margin + 3, Pos.middle + 10), this.font),
					//new Text("Ovalidad IZ", new Vector(Pos.beg_margin, Pos.three_quarters), this.font),
					//new Text(`${OI}`, new Vector(Pos.beg_margin + 3, Pos.three_quarters + 10), this.font),
					new Text("Diferencia", new Vector(Pos.middle - 10, Pos.middle), this.font),
					new Text(`${DFT} Kg`, new Vector(Pos.middle - 5, Pos.middle + 10), this.font),
					new Text("Peso del Eje ", new Vector(Pos.middle - 10, Pos.quarter), this.font),
					new Text(`${peso} %`, new Vector(Pos.middle - 5, Pos.quarter + 10), this.font),
					//new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.quarter), this.font),
					//new Text(`${FD} KN`, new Vector(Pos.three_quarters + 3, Pos.quarter + 10), this.font),
					new Text("Peso Lado Derecho", new Vector(Pos.three_quarters, Pos.quarter), this.font),
					new Text(`${Pd} KG`, new Vector(Pos.three_quarters + 3, Pos.quarter + 10), this.font),
					//new Text("Res.Rodadura DE", new Vector(Pos.three_quarters, Pos.middle), this.font),
					//new Text(`${RD} Kg`, new Vector(Pos.three_quarters + 3, Pos.middle + 10), this.font),
					new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.middle), this.font),
					new Text(`${FD} KN`, new Vector(Pos.three_quarters + 3, Pos.middle + 10), this.font)
					//new Text("Ovalidad DE", new Vector(Pos.three_quarters, Pos.three_quarters), this.font),
					//new Text(`${OD}`, new Vector(Pos.three_quarters + 3, Pos.three_quarters + 10), this.font)
				]);
			}
		};
		let contador = 0
		for (let i = 0; i < ejes.length; i++) {
			if (data[`peso_estatico_${i + 1}`] != '---') {
				contador = contador + 15
				table(
					ejes[i],
					i + 1,
					data[`fuerza_izquierda_${i + 1}`],
					data[`resistencia_izquierda_${i + 1}`],
					data[`ovalidad_izquierda_${i + 1}`],
					data[`peso_estatico_${i + 1}`],
					data[`fuerza_derecha_${i + 1}`],
					data[`resistencia_derecha_${i + 1}`],
					data[`ovalidad_derecha_${i + 1}`],
					data[`diferencia_freno_${i + 1}`],
					data[`peso_izquierdo_${i + 1}`],
					data[`peso_derecho_${i + 1}`],
					);
			//console.log(data);
			}
		}

		return pointer.addxy(0, contador);
	}

	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(`${this.Dominio} - Diagnóstico de Frenos - Eje Trasero - Frenos de Mano`, new Vector(Pos.beg, Pos.third -25, ), this.font),

			new Text("Peso Lado Izquierdo", new Vector(Pos.beg_margin, Pos.quarter), this.font),
			new Text(`${data['peso_estatico']} Kg`, new Vector(Pos.beg_margin + 3, Pos.quarter + 15), this.font),
			new Text("Peso del Eje ", new Vector(Pos.middle - 10, Pos.quarter), this.font),
			new Text(`${data['peso_estatico']} Kg`, new Vector(Pos.middle - 5, Pos.quarter +15), this.font),
			new Text("Peso Lado Derecho", new Vector(Pos.three_quarters, Pos.quarter), this.font),
			new Text(`${data['peso_estatico']} KG`, new Vector(Pos.three_quarters + 3, Pos.quarter + 15), this.font),
			
			new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.middle + 5), this.font),
			new Text(`${data['fuerza_izquierda']} KN`, new Vector(Pos.beg_margin + 3, Pos.middle + 20), this.font),
			new Text("Diferencia", new Vector(Pos.middle - 10, Pos.middle +5), this.font),
			new Text(`${data['diferencia_freno_mano']} Kg`, new Vector(Pos.middle - 5, Pos.middle + 20), this.font),
			new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.middle +5), this.font),
			new Text(`${data['fuerza_derecha']} KN`, new Vector(Pos.three_quarters + 3, Pos.middle + 20), this.font)



			//new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.two_thirds), this.font),
			//new Text(data['fuerza_izquierda'], new Vector(Pos.beg_margin, Pos.end), this.font),
			//new Text("Fuerza DE", new Vector(Pos.three_quarters, Pos.two_thirds), this.font),
			//new Text(data['fuerza_derecha'], new Vector(Pos.three_quarters, Pos.end), this.font),

		]);

		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)));
		if (data['opacidad_logaritmica'] != '---') {
			gaseshumos.addRelativeChildren([
				new Text(`${this.Dominio} - Resultado Analizador de Gases`, new Vector(Pos.beg, Pos.third), this.font),
				new Text(`Medición    ${data['opacidad_logaritmica']}`, new Vector(Pos.three_quarters, Pos.two_thirds), this.font)
			]);
		}
		if (data['co'] != '---') {
			gaseshumos.addRelativeChildren([
				new Text(`${this.Dominio} - Resultado Analizador de Humos`, new Vector(Pos.two_thirds, Pos.third), this.font),
				new Text(`CO    ${data['co']} %        HC    ${data['hc']} ppm`, new Vector(Pos.beg, Pos.two_thirds), this.font),
			]);
		}
		return pointer.addxy(0, 9);
	}

	ruidoToPDF(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(`${this.Dominio} - Resultado Analizador Ruido`, new Vector(Pos.beg, Pos.third), this.font),
		
		new Text("", new Vector(Pos.two_thirds, Pos.third), this.font),
		]);
		gaseshumos.addRelativeChildren([
			new Text(`     ${data['nivelsonoro']} dba`, new Vector(Pos.beg, Pos.two_thirds), this.font),

			new Text("", new Vector(Pos.three_quarters, Pos.two_thirds), this.font)

		]);

		return pointer.addxy(0, 9);
	}


}