pdf2.js 7.07 KB
class PDF2 {

	"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;
		this.font = {
			"font": "courier",
			"type": "normal",
			"size": 10
		};
	}


	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, 5)));

		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);
	}

	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);
	}

	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, ), 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)
			]);
		};

		for (let i = 0; i < 2; i++) {
			table(
				ejes[i],
				i + 1,
				data[`rendimiento_izquierdo_${i+1}`],
				data[`peso_estatico_${i+1}`],
				data[`rendimiento_derecho_${i+1}`]
			);
		}

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

				new Text("Fuerza IZ", new Vector(Pos.beg_margin, Pos.quarter), this.font),

				new Text(`${FI}`, new Vector(Pos.beg_margin + 3, Pos.quarter + 10), this.font),

				new Text("Res.Rodadura IZ", new Vector(Pos.beg_margin, Pos.middle), this.font),

				new Text(`${RI}`, 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("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}`, 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}`, 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)

			]);
		};

		for (let i = 0; i < ejes.length; i++) {
			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}`]
			);
		}

		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, ), 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)));

		gaseshumos.addRelativeChildren([

			new Text("Resultado Analizador de Gases", new Vector(Pos.beg, Pos.third), this.font),

			new Text("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),

			new Text(`Medición    ${data['opacidad_logaritmica']}`, new Vector(Pos.three_quarters, Pos.two_thirds), this.font)

		]);

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