Robust text renderer using headless chrome.
This module is also available as a CLI.
ImageMagick is the traditional unix tool to programatically render text, and while it works very well for simple use cases, trying to use it to render rich text or html is very difficult. Pango is another option that's been around for ages, but both suffer from archaic syntax and minimal rich text support.
Puppeteer, on the other hand, allows for robust, headless chrome screenshots with best-in-class support for all modern html / text / font features.
This module makes it easy to use headless chrome to render text + html to images.
- built-in fontfaceobserver
- easily load google fonts
- optional word-wrap
- main content is just html
- styling is done via css
- handles multiple fonts
- thoroughly tested
- includes a CLI
npm install --save puppeteer-render-text
const renderText =// render text with built-in font and no word-wrapawait// render text with custom google font and word-wrap at 400pxawait// render html with custom google font and custom word-wrap at 100pxawait
Renders the given text / html via puppeteer.
Asynchronously returns the generated html page as a string for debugging purposes.
If you want to load multiple google fonts, juse specify their font-families in
separated by commas as you normally would for CSS fonts.
function (opts): Promise
optsobject Configuration options
opts.textstring HTML content to render
opts.outputstring Path of image file to output result
opts.widthnumber? Optional max width for word-wrap
opts.heightnumber? Optional max height to clip overflow
opts.loadFontFamilystring? Optional font family to load with fontfaceobserver
opts.loadGoogleFontboolean Whether or not to load and wait for
opts.style.fontFamilyas one or more google fonts (optional, default
opts.styleobject JS CSS styles to apply to the text's container div (optional, default
opts.injectobject Optionally injects arbitrary string content into the head, style, or body elements. (optional, default