Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Remove playwrite, use puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 62ac626 commit 66e63e5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 71 deletions.
10 changes: 0 additions & 10 deletions src/providers/playwright/playwright.module.ts

This file was deleted.

61 changes: 0 additions & 61 deletions src/providers/playwright/playwright.service.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/providers/puppeteer/puppeteer.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
38 changes: 38 additions & 0 deletions src/providers/puppeteer/puppeteer.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 66e63e5

Please sign in to comment.