-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use puppeteer to convert html to png
- Loading branch information
Showing
8 changed files
with
600 additions
and
381 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,27 @@ | ||
import puppeteer, { ScreenshotClip } from "puppeteer"; | ||
|
||
const ref = { | ||
browser: null, | ||
page: null, | ||
}; | ||
|
||
export const getBrowser = async () => { | ||
if (ref.browser) return ref.browser; | ||
|
||
ref.browser = await puppeteer.launch({ headless: "new" }); | ||
return ref.browser; | ||
}; | ||
|
||
export const getPage = async () => { | ||
if (ref.page) return ref.page; | ||
|
||
ref.page = await (await getBrowser()).newPage(); | ||
return ref.page; | ||
}; | ||
|
||
export const getScreenshotBuffer = async (html: string, clip: ScreenshotClip) => { | ||
const page = await getPage(); | ||
await page.setContent(html); | ||
|
||
return page.screenshot({ clip }); | ||
}; |
Oops, something went wrong.