This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62ac626
commit 66e63e5
Showing
4 changed files
with
48 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |