mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-18 02:31:56 +10:00
🚀 release v3.0.0
This commit is contained in:
86
server/src/printer/printer.service.ts
Normal file
86
server/src/printer/printer.service.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { SchedulerRegistry } from '@nestjs/schedule';
|
||||
import { mkdir, unlink, writeFile } from 'fs/promises';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { join } from 'path';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { Browser, chromium } from 'playwright-chromium';
|
||||
|
||||
export const DELETION_TIME = 10 * 1000; // 10 seconds
|
||||
|
||||
@Injectable()
|
||||
export class PrinterService implements OnModuleInit, OnModuleDestroy {
|
||||
private browser: Browser;
|
||||
|
||||
constructor(private readonly schedulerRegistry: SchedulerRegistry, private readonly configService: ConfigService) {}
|
||||
|
||||
async onModuleInit() {
|
||||
this.browser = await chromium.launch();
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.browser.close();
|
||||
}
|
||||
|
||||
async printAsPdf(username: string, slug: string): Promise<string> {
|
||||
const url = this.configService.get<string>('app.url');
|
||||
const secretKey = this.configService.get<string>('app.secretKey');
|
||||
const serverUrl = this.configService.get<string>('app.serverUrl');
|
||||
|
||||
const page = await this.browser.newPage();
|
||||
|
||||
await page.goto(`${url}/${username}/${slug}/printer?secretKey=${secretKey}`);
|
||||
await page.waitForSelector('html.wf-active');
|
||||
|
||||
const resumePages = await page.$$eval('[data-page]', (pages) => {
|
||||
return pages.map((page, index) => ({
|
||||
pageNumber: index + 1,
|
||||
innerHTML: page.innerHTML,
|
||||
height: page.clientHeight,
|
||||
}));
|
||||
});
|
||||
|
||||
const pdf = await PDFDocument.create();
|
||||
const directory = join(__dirname, 'assets', 'resumes');
|
||||
const filename = `RxResume_PDFExport_${nanoid()}.pdf`;
|
||||
const publicUrl = `${serverUrl}/resumes/${filename}`;
|
||||
|
||||
for (let index = 0; index < resumePages.length; index++) {
|
||||
await page.evaluate((page) => (document.body.innerHTML = page.innerHTML), resumePages[index]);
|
||||
|
||||
const buffer = await page.pdf({
|
||||
width: '210mm',
|
||||
printBackground: true,
|
||||
height: resumePages[index].height,
|
||||
});
|
||||
|
||||
const pageDoc = await PDFDocument.load(buffer);
|
||||
const copiedPages = await pdf.copyPages(pageDoc, [0]);
|
||||
|
||||
copiedPages.forEach((copiedPage) => pdf.addPage(copiedPage));
|
||||
}
|
||||
|
||||
await page.close();
|
||||
|
||||
const pdfBytes = await pdf.save();
|
||||
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(join(directory, filename), pdfBytes);
|
||||
|
||||
// Delete PDF artifacts after DELETION_TIME ms
|
||||
const timeout = setTimeout(async () => {
|
||||
try {
|
||||
await unlink(join(directory, filename));
|
||||
|
||||
this.schedulerRegistry.deleteTimeout(`delete-${filename}`);
|
||||
} catch {
|
||||
// pass through
|
||||
}
|
||||
}, DELETION_TIME);
|
||||
|
||||
this.schedulerRegistry.addTimeout(`delete-${filename}`, timeout);
|
||||
|
||||
return publicUrl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user