refactor: remove Puppeteer, delete old PDF download endpoint
This commit is contained in:
@@ -44,7 +44,6 @@
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
"pdfkit": "^0.18.0",
|
||||
"puppeteer": "^25.3.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"typeorm": "^0.3.28"
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import puppeteer from 'puppeteer';
|
||||
import { Response } from 'express';
|
||||
import { StudentProfile } from '../entities/student-profile.entity';
|
||||
import { StudentEnrollment } from '../entities/student-enrollment.entity';
|
||||
import { ExamScore } from '../entities/exam-score.entity';
|
||||
@@ -33,57 +31,6 @@ export class ArchiveReportService {
|
||||
@InjectRepository(Student) private studentRepo: Repository<Student>,
|
||||
) {}
|
||||
|
||||
async generateReport(studentId: number, res: Response): Promise<void> {
|
||||
const [student, profile, enrollments, exams, learnings, result, attendances] =
|
||||
await Promise.all([
|
||||
this.studentRepo.findOne({ where: { id: studentId } }),
|
||||
this.profileRepo.findOne({ where: { studentId } }),
|
||||
this.enrollmentRepo.find({ where: { studentId }, order: { startDate: 'ASC' } }),
|
||||
this.examRepo.find({ where: { studentId }, order: { examDate: 'ASC' } }),
|
||||
this.learningRepo.find({ where: { studentId }, order: { recordDate: 'DESC' } }),
|
||||
this.resultRepo.findOne({ where: { studentId } }),
|
||||
this.attendanceRepo.find({ where: { studentId }, order: { attendanceDate: 'ASC' } }),
|
||||
]);
|
||||
|
||||
if (!student) throw new Error('学生不存在');
|
||||
|
||||
const data: ReportData = {
|
||||
student,
|
||||
profile,
|
||||
enrollments,
|
||||
exams,
|
||||
learnings,
|
||||
result,
|
||||
attendances,
|
||||
};
|
||||
|
||||
const html = this.buildHtml(data);
|
||||
|
||||
const browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
||||
});
|
||||
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await page.setContent(html, { waitUntil: 'load' });
|
||||
const pdf = await page.pdf({
|
||||
format: 'A4',
|
||||
printBackground: true,
|
||||
margin: { top: 0, right: 0, bottom: 0, left: 0 },
|
||||
});
|
||||
|
||||
res.setHeader('Content-Type', 'application/pdf');
|
||||
res.setHeader(
|
||||
'Content-Disposition',
|
||||
`attachment; filename=student_report_${studentId}.pdf`,
|
||||
);
|
||||
res.end(pdf);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
async generateReportHtml(studentId: number): Promise<string> {
|
||||
const [student, profile, enrollments, exams, learnings, result, attendances] =
|
||||
await Promise.all([
|
||||
|
||||
@@ -8,12 +8,11 @@ import {
|
||||
Param,
|
||||
UseGuards,
|
||||
Request,
|
||||
Res,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import type { Request as ExpressRequest, Response } from 'express';
|
||||
import type { Request as ExpressRequest } from 'express';
|
||||
import { ArchiveReportService } from './archive-report.service';
|
||||
import { ArchiveService } from './archive.service';
|
||||
import {
|
||||
@@ -340,26 +339,6 @@ export class ArchiveController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Get(':studentId/report')
|
||||
@RequirePermission('student:view')
|
||||
async generateReport(
|
||||
@Param('studentId') studentId: string,
|
||||
@Res() res: Response,
|
||||
@Request() req: AuthenticatedRequest,
|
||||
) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
await this.logService.log({
|
||||
userId: req.user?.id,
|
||||
username: req.user?.username,
|
||||
module: 'archive',
|
||||
action: 'generate_report',
|
||||
targetId: +studentId,
|
||||
targetType: 'student',
|
||||
ipAddress,
|
||||
userAgent,
|
||||
});
|
||||
return this.reportService.generateReport(+studentId, res);
|
||||
}
|
||||
|
||||
@Get(':studentId/report-html')
|
||||
@RequirePermission('student:view')
|
||||
|
||||
Reference in New Issue
Block a user