npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

puppeteer-render-text

v2.1.3

Published

Robust text renderer using headless chrome.

Downloads

74

Readme

puppeteer-render-text

Robust text renderer using headless chrome.

NPM Build Status MIT License Prettier Code Formatting

This module is also available as a CLI.

Why?

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.

Features

Install

npm install puppeteer-render-text

Usage

import { renderText } from 'puppeteer-render-text'

// render text with built-in font and no word-wrap
await renderText({
  text: 'hello world',
  output: 'out0.png',
  style: {
    fontFamily: 'segue ui',
    fontSize: 64
  }
})

// render text with custom google font and word-wrap at 400px
await renderText({
  text: 'headless chrome is awesome',
  output: 'out1.png',
  loadGoogleFont: true,
  width: 400,
  style: {
    fontFamily: 'Roboto',
    fontSize: 32,
    padding: 16
  }
})

// render html with custom google font and custom word-wrap at 100px
await renderText({
  text: 'headless <b>chrome</b> is <span style="color: red: font-style: italic;">awesome</span>',
  output: 'out1.png',
  loadGoogleFont: true,
  width: 100,
  style: {
    fontFamily: 'Roboto',
    overflowWrap: 'break-word'
  }
})

Note that all CSS styles are specified via the JS CSS syntax, which uses camelCase instead of hyphens. This is, for instance, what React uses for its inline styles.

API

renderText

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 opts.style.fontFamily separated by commas as you normally would for CSS fonts.

Type: function (opts): Promise

  • opts object Configuration options
    • opts.text string HTML content to render
    • opts.output string Path of image file to output result
    • opts.width number? Optional max width for word-wrap
    • opts.height number? Optional max height to clip overflow
    • opts.loadFontFamily string? Optional font family to load with fontfaceobserver
    • opts.loadGoogleFont boolean Whether or not to load and wait for opts.style.fontFamily as one or more google fonts (optional, default false)
    • opts.style object JS CSS styles to apply to the text's container div (optional, default {})
    • opts.inject object Optionally injects arbitrary string content into the head, style, or body elements. (optional, default {})
      • opts.inject.head string? Optionally injected into the document
      • opts.inject.style string? Optionally injected into a tag within the document
      • opts.inject.body string? Optionally injected into the document

Related

License

MIT © Travis Fischer

If you found this project interesting, please consider sponsoring me or following me on twitter