Created
June 1, 2020 14:23
-
-
Save Terance98/8a8fb5ffb105076601d6ae378236f148 to your computer and use it in GitHub Desktop.
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
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