What is the easiest way to render a raw HTML string, such as:
<b>Hello World</b>
into a standard Node.js Buffer as a PNG image?
I've been searching all over npm for a module that'd let me do this, but I haven't been able to find one. They all seem to render directly into a file.
One way to do this is to simulate a browser environment using something like puppeteer and then use its screenshot/printing functions to create the PNG file.
Some code (after the puppeteer docu - not tested):
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent( yourHTMLCode );
await page.screenshot({path: 'example.png'});
await browser.close();
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With