mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 16:51:33 +10:00
Add PDF file caching
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query } from '@nestjs/common';
|
||||
|
||||
import { PrinterService } from './printer.service';
|
||||
|
||||
@ -7,7 +7,7 @@ export class PrinterController {
|
||||
constructor(private readonly printerService: PrinterService) {}
|
||||
|
||||
@Get('/:username/:slug')
|
||||
printAsPdf(@Param('username') username: string, @Param('slug') slug: string): Promise<string> {
|
||||
return this.printerService.printAsPdf(username, slug);
|
||||
printAsPdf(@Param('username') username: string, @Param('slug') slug: string, @Query('lastUpdated') lastUpdated: string): Promise<string> {
|
||||
return this.printerService.printAsPdf(username, slug, lastUpdated);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { SchedulerRegistry } from '@nestjs/schedule';
|
||||
import { PageConfig } from '@reactive-resume/schema';
|
||||
import { mkdir, unlink, writeFile } from 'fs/promises';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { access, mkdir, readdir, unlink, writeFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { Browser, chromium } from 'playwright-chromium';
|
||||
@ -14,7 +12,7 @@ export const DELETION_TIME = 10 * 1000; // 10 seconds
|
||||
export class PrinterService implements OnModuleInit, OnModuleDestroy {
|
||||
private browser: Browser;
|
||||
|
||||
constructor(private readonly schedulerRegistry: SchedulerRegistry, private readonly configService: ConfigService) {}
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
async onModuleInit() {
|
||||
this.browser = await chromium.launch({
|
||||
@ -26,68 +24,70 @@ export class PrinterService implements OnModuleInit, OnModuleDestroy {
|
||||
await this.browser.close();
|
||||
}
|
||||
|
||||
async printAsPdf(username: string, slug: string): Promise<string> {
|
||||
const url = this.configService.get<string>('app.url');
|
||||
async printAsPdf(username: string, slug: string, lastUpdated: string): Promise<string> {
|
||||
const serverUrl = this.configService.get<string>('app.serverUrl');
|
||||
const secretKey = this.configService.get<string>('app.secretKey');
|
||||
|
||||
const page = await this.browser.newPage();
|
||||
|
||||
await page.goto(`${url}/${username}/${slug}/printer?secretKey=${secretKey}`);
|
||||
await page.waitForSelector('html.wf-active');
|
||||
|
||||
const pageFormat: PageConfig['format'] = await page.$$eval(
|
||||
'[data-page]',
|
||||
(pages) => pages[0].getAttribute('data-format') as PageConfig['format']
|
||||
);
|
||||
|
||||
const resumePages = await page.$$eval('[data-page]', (pages) =>
|
||||
pages.map((page, index) => ({
|
||||
pageNumber: index + 1,
|
||||
innerHTML: page.innerHTML,
|
||||
height: page.clientHeight,
|
||||
}))
|
||||
);
|
||||
|
||||
const pdf = await PDFDocument.create();
|
||||
const directory = join(__dirname, '..', 'assets/exports');
|
||||
const filename = `RxResume_PDFExport_${nanoid()}.pdf`;
|
||||
const filename = `RxResume_PDFExport_${username}_${slug}_${lastUpdated}.pdf`;
|
||||
const publicUrl = `${serverUrl}/assets/exports/${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({
|
||||
printBackground: true,
|
||||
height: resumePages[index].height,
|
||||
width: pageFormat === 'A4' ? '210mm' : '216mm',
|
||||
try { // check if file already exists
|
||||
await access(join(directory, filename));
|
||||
} catch { // create file as it doesn't exist
|
||||
// delete old files
|
||||
await readdir(directory).then(async (files) => {
|
||||
await Promise.all(files.map(async (file) => {
|
||||
if (file.startsWith(`RxResume_PDFExport_${username}_${slug}`)) {
|
||||
await unlink(join(directory, file));
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
const pageDoc = await PDFDocument.load(buffer);
|
||||
const copiedPages = await pdf.copyPages(pageDoc, [0]);
|
||||
const url = this.configService.get<string>('app.url');
|
||||
const secretKey = this.configService.get<string>('app.secretKey');
|
||||
|
||||
copiedPages.forEach((copiedPage) => pdf.addPage(copiedPage));
|
||||
}
|
||||
const page = await this.browser.newPage();
|
||||
|
||||
await page.close();
|
||||
await page.goto(`${url}/${username}/${slug}/printer?secretKey=${secretKey}`);
|
||||
await page.waitForSelector('html.wf-active');
|
||||
|
||||
const pdfBytes = await pdf.save();
|
||||
const pageFormat: PageConfig['format'] = await page.$$eval(
|
||||
'[data-page]',
|
||||
(pages) => pages[0].getAttribute('data-format') as PageConfig['format']
|
||||
);
|
||||
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(join(directory, filename), pdfBytes);
|
||||
const resumePages = await page.$$eval('[data-page]', (pages) =>
|
||||
pages.map((page, index) => ({
|
||||
pageNumber: index + 1,
|
||||
innerHTML: page.innerHTML,
|
||||
height: page.clientHeight,
|
||||
}))
|
||||
);
|
||||
|
||||
// Delete PDF artifacts after DELETION_TIME ms
|
||||
const timeout = setTimeout(async () => {
|
||||
try {
|
||||
await unlink(join(directory, filename));
|
||||
const pdf = await PDFDocument.create();
|
||||
|
||||
this.schedulerRegistry.deleteTimeout(`delete-${filename}`);
|
||||
} catch {
|
||||
// pass through
|
||||
for (let index = 0; index < resumePages.length; index++) {
|
||||
await page.evaluate((page) => (document.body.innerHTML = page.innerHTML), resumePages[index]);
|
||||
|
||||
const buffer = await page.pdf({
|
||||
printBackground: true,
|
||||
height: resumePages[index].height,
|
||||
width: pageFormat === 'A4' ? '210mm' : '216mm',
|
||||
});
|
||||
|
||||
const pageDoc = await PDFDocument.load(buffer);
|
||||
const copiedPages = await pdf.copyPages(pageDoc, [0]);
|
||||
|
||||
copiedPages.forEach((copiedPage) => pdf.addPage(copiedPage));
|
||||
}
|
||||
}, DELETION_TIME);
|
||||
|
||||
this.schedulerRegistry.addTimeout(`delete-${filename}`, timeout);
|
||||
await page.close();
|
||||
|
||||
const pdfBytes = await pdf.save();
|
||||
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(join(directory, filename), pdfBytes);
|
||||
}
|
||||
|
||||
return publicUrl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user