envio de constancias pdf, se añadio banner
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
interface Row {
|
||||
Asistencia: number
|
||||
Nombre: string
|
||||
'Apellido Paterno': string
|
||||
'Apellido Materno': string
|
||||
Carrera: string
|
||||
'Institución Procedencia': string
|
||||
Email: string
|
||||
}
|
||||
@@ -1,6 +1,21 @@
|
||||
import { Body, Controller, Param, ParseIntPipe, Post } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Res,
|
||||
UploadedFile,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { EmailDto } from './dto/emailDto.dto';
|
||||
import { EmailService } from './email.service';
|
||||
import { Response } from 'express';
|
||||
import * as xlsx from 'xlsx';
|
||||
|
||||
|
||||
@Controller('email')
|
||||
export class EmailController {
|
||||
@@ -13,4 +28,14 @@ export class EmailController {
|
||||
): Promise<any> {
|
||||
return this.emailService.sendEmail(id, emailDto);
|
||||
}
|
||||
|
||||
@Post('constancias')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async sendConstancias(@UploadedFile() file, @Body('nombreEvento') nombreEvento: string,) {
|
||||
if(!file) {
|
||||
throw new HttpException("No se ha cargado ningun archivo", HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
console.log(file)
|
||||
return this.emailService.sendConstancias(file, nombreEvento);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Inject, Injectable, Res, UploadedFile } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
@@ -6,6 +6,13 @@ import { EventosService } from 'src/eventos/eventos.service';
|
||||
import { Participante } from 'src/participante/participante.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { EmailDto } from './dto/emailDto.dto';
|
||||
import * as xlsx from 'xlsx';
|
||||
import * as pdfMake from 'pdfmake/build/pdfmake';
|
||||
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
|
||||
/* import * as PDFDocument from 'pdfkit'; */
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';
|
||||
|
||||
@Injectable()
|
||||
export class EmailService {
|
||||
@@ -49,4 +56,80 @@ export class EmailService {
|
||||
|
||||
return participantes;
|
||||
}
|
||||
|
||||
async sendConstancias(file, nombreEvento) {
|
||||
console.log(file)
|
||||
const workbook = xlsx.read(file.buffer);
|
||||
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
const data = xlsx.utils.sheet_to_json(worksheet);
|
||||
|
||||
for (const row of data as Row[]) {
|
||||
const pdfDoc = await PDFDocument.create();
|
||||
const page = pdfDoc.addPage();
|
||||
page.setSize(792, 612);
|
||||
|
||||
page.drawRectangle({
|
||||
x: 0,
|
||||
y: page.getHeight() - 60,
|
||||
width: page.getWidth(),
|
||||
height: 60,
|
||||
color: rgb(0 / 255, 61 / 255, 121 / 255),
|
||||
});
|
||||
|
||||
page.drawRectangle({
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: page.getWidth(),
|
||||
height: 60,
|
||||
color: rgb(213 / 255, 159 / 255, 15 / 255),
|
||||
});
|
||||
|
||||
const font = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
|
||||
const fontSize = 30;
|
||||
const text = 'Constancia de asistencia';
|
||||
const textWidth = font.widthOfTextAtSize(text, fontSize);
|
||||
const textHeight = font.heightAtSize(fontSize);
|
||||
|
||||
page.drawText(text, {
|
||||
x: (page.getWidth() - textWidth) / 2,
|
||||
y: (page.getHeight() - textHeight) / 2,
|
||||
size: fontSize,
|
||||
font: font,
|
||||
color: rgb(0/255, 0/255, 0/255),
|
||||
});
|
||||
|
||||
const { width, height } = page.getSize();
|
||||
page.drawText(
|
||||
`Se le otorga la constancia por su asistencia al evento: ${nombreEvento}`,
|
||||
{
|
||||
x: 50,
|
||||
y: height - 50,
|
||||
size: 12,
|
||||
font: await pdfDoc.embedFont('Helvetica'),
|
||||
},
|
||||
);
|
||||
page.drawText(
|
||||
`A ${row['Apellido Paterno']} ${row['Apellido Materno']} ${row.Nombre}`,
|
||||
{
|
||||
x: 50,
|
||||
y: height - 80,
|
||||
size: 12,
|
||||
font: await pdfDoc.embedFont('Helvetica'),
|
||||
},
|
||||
);
|
||||
const pdfBytes = await pdfDoc.save();
|
||||
const attachment = {
|
||||
filename: `Constancia ${nombreEvento} - ${row.Nombre}_${row['Apellido Paterno']}.pdf`,
|
||||
content: pdfBytes,
|
||||
};
|
||||
/* const blob = new Blob([pdfBytes], { type: 'application/pdf' });
|
||||
const url = URL.createObjectURL(blob); */
|
||||
this.transporter.sendMail({
|
||||
to: row.Email,
|
||||
subject: 'Constancia de asistencia',
|
||||
text: 'Hola',
|
||||
attachments: [attachment],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user