diff --git a/src/providers/playwright/playwright.module.ts b/src/providers/playwright/playwright.module.ts deleted file mode 100644 index a4d6ab9cd..000000000 --- a/src/providers/playwright/playwright.module.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Module } from '@nestjs/common'; -import { ConfigModule } from '@nestjs/config'; -import { PlaywrightService } from './playwright.service'; - -@Module({ - imports: [ConfigModule], - providers: [PlaywrightService], - exports: [PlaywrightService], -}) -export class PlaywrightModule {} diff --git a/src/providers/playwright/playwright.service.ts b/src/providers/playwright/playwright.service.ts deleted file mode 100644 index d575e2591..000000000 --- a/src/providers/playwright/playwright.service.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { chromium, firefox, webkit } from 'playwright'; - -@Injectable() -export class PlaywrightService { - chromium = chromium; - firefox = firefox; - webkit = webkit; - - async renderHtmlToImage( - html: string, - viewport?: { width: number; height: number }, - ) { - const browser = await chromium.launch({ - executablePath: process.env.CHROMIUM_PATH, - args: ['--no-sandbox'], - }); - const page = await browser.newPage(); - if (viewport) await page.setViewportSize(viewport); - await page.setContent(html, { waitUntil: 'networkidle' }); - const screenshot = await page.screenshot(); - await browser.close(); - return screenshot; - } - - async renderHtmlToPdf( - html: string, - viewport?: { width: number; height: number }, - options?: { - path?: string; - scale?: number; - displayHeaderFooter?: boolean; - headerTemplate?: string; - footerTemplate?: string; - printBackground?: boolean; - landscape?: boolean; - pageRanges?: string; - format?: string; - width?: string | number; - height?: string | number; - margin?: { - top?: string | number; - right?: string | number; - bottom?: string | number; - left?: string | number; - }; - preferCSSPageSize?: boolean; - }, - ) { - const browser = await chromium.launch({ - executablePath: process.env.CHROMIUM_PATH, - args: ['--no-sandbox'], - }); - const page = await browser.newPage(); - if (viewport) await page.setViewportSize(viewport); - await page.setContent(html, { waitUntil: 'networkidle' }); - const file = await page.pdf(options); - await browser.close(); - return file; - } -} diff --git a/src/providers/puppeteer/puppeteer.module.ts b/src/providers/puppeteer/puppeteer.module.ts new file mode 100644 index 000000000..63c21b28c --- /dev/null +++ b/src/providers/puppeteer/puppeteer.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { PuppeteerService } from './puppeteer.service'; + +@Module({ + imports: [ConfigModule], + providers: [PuppeteerService], + exports: [PuppeteerService], +}) +export class PuppeteerModule {} diff --git a/src/providers/puppeteer/puppeteer.service.ts b/src/providers/puppeteer/puppeteer.service.ts new file mode 100644 index 000000000..0e0921147 --- /dev/null +++ b/src/providers/puppeteer/puppeteer.service.ts @@ -0,0 +1,38 @@ +import { Injectable } from '@nestjs/common'; +import { launch, PDFOptions } from 'puppeteer'; + +@Injectable() +export class PuppeteerService { + async renderHtmlToImage( + html: string, + viewport?: { width: number; height: number }, + ) { + const browser = await launch({ + executablePath: process.env.CHROMIUM_PATH, + args: ['--no-sandbox'], + }); + const page = await browser.newPage(); + if (viewport) await page.setViewport(viewport); + await page.setContent(html, { waitUntil: 'networkidle2' }); + const screenshot = await page.screenshot({ encoding: 'binary' }); + await browser.close(); + return screenshot; + } + + async renderHtmlToPdf( + html: string, + viewport?: { width: number; height: number }, + options?: PDFOptions, + ) { + const browser = await launch({ + executablePath: process.env.CHROMIUM_PATH, + args: ['--no-sandbox'], + }); + const page = await browser.newPage(); + if (viewport) await page.setViewport(viewport); + await page.setContent(html, { waitUntil: 'networkidle2' }); + const file = await page.pdf(options); + await browser.close(); + return file; + } +}