Skip to content

Instantly share code, notes, and snippets.

@Terance98
Created June 1, 2020 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Terance98/8a8fb5ffb105076601d6ae378236f148 to your computer and use it in GitHub Desktop.
Save Terance98/8a8fb5ffb105076601d6ae378236f148 to your computer and use it in GitHub Desktop.
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const rootPath = process.cwd();
const fs = require('fs');
const mkdirp = require('mkdirp')
const puppeteer = require('puppeteer');
const EjsToHtml = require('./EjsToHtml');
const convert = async (templateAttributes) => {
try {
if (!fs.existsSync(path.join(rootPath, 'assets', 'images', 'AllCertificates'))) {
mkdirp.sync(path.join(rootPath, 'assets', 'images', 'AllCertificates'));
}
const htmlString = await EjsToHtml.generate(templateAttributes);
const uniqueImageName = String(uuidv4())+'.png';
const permanentPath = path.join('assets', 'images', 'AllCertificates', uniqueImageName);
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-web-security'] });
const page = await browser.newPage();
await page.setContent(htmlString, { waitUntil: 'networkidle0', timeout: 25000 });
await page.setViewport({ width: 1920, height: 1080 });
await page.screenshot({ path: permanentPath });
await browser.close();
return permanentPath
} catch (err) {
throw err;
}
}
module.exports = { convert };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment