3

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.

2
  • 1
    Possibly using a headless browser and its printing function. So look at puppeteer or similar. Commented Jul 17, 2018 at 4:06
  • 1
    @Sirko That's exactly what I was looking for. Thank you so much! I wish you replied with an answer so I could credit you. :) Commented Jul 17, 2018 at 4:16

1 Answer 1

8

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();
})();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.