Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable } from '@nestjs/common';
import * as Handlebars from 'handlebars';
import puppeteer, { Browser, Page } from 'puppeteer';
@Injectable()
export class PdfService {
async render(template: string, data: any): Promise<Uint8Array> {
// Compile the Handlebars template
const compiledTemplate: HandlebarsTemplateDelegate = Handlebars.compile(template);
const htmlContent: string = compiledTemplate(data);
// Generate PDF using Puppeteer
const browser: Browser = await puppeteer.launch({ headless: 'shell' });
const page: Page = await browser.newPage();
await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
const pdfBuffer: Uint8Array = await page.pdf({ format: 'A4' });
await browser.close();
return pdfBuffer;
}
}
|