Skip to content

Commit

Permalink
refactor: use puppeteer to convert html to png
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Mar 4, 2024
1 parent ae8401b commit 6936e37
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 381 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"drizzle-orm": "^0.27.2",
"express": "^4.18.2",
"galeforce": "^0.6.1",
"mustache": "^4.2.0",
"node-cron": "^3.0.2",
"node-html-to-image": "^4.0.0",
"pastable": "^2.2.0",
"postgres": "^3.3.5",
"puppeteer": "^20.9.0",
Expand All @@ -45,6 +47,7 @@
"@types/cors": "^2.8.13",
"@types/debug": "^4.1.7",
"@types/express": "^4.17.17",
"@types/mustache": "^4.2.5",
"@types/node": "^16.4.1",
"@types/node-cron": "^3.0.7",
"cross-env": "^7.0.3",
Expand Down
131 changes: 129 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/features/details/browser.ts
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 });
};
Loading

0 comments on commit 6936e37

Please sign in to comment.