All Projects → frinyvonnick → Node Html To Image

frinyvonnick / Node Html To Image

Licence: apache-2.0
A Node.js module that generates images from HTML

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Html To Image

Imgcat
It's like cat, but for images.
Stars: ✭ 577 (+64.39%)
Mutual labels:  hacktoberfest, image
Goimagehash
Go Perceptual image hashing package
Stars: ✭ 391 (+11.4%)
Mutual labels:  hacktoberfest, image
Yii2 Imagine
Yii 2 imagine extension
Stars: ✭ 271 (-22.79%)
Mutual labels:  hacktoberfest, image
Imgcat
a tool to output images as RGB ANSI graphics on the terminal
Stars: ✭ 425 (+21.08%)
Mutual labels:  hacktoberfest, image
Imagemagick
🧙‍♂️ ImageMagick 7
Stars: ✭ 6,400 (+1723.36%)
Mutual labels:  hacktoberfest, image
Magick.net
The .NET library for ImageMagick
Stars: ✭ 2,071 (+490.03%)
Mutual labels:  hacktoberfest, image
Kiimagepager
The KIImagePager is inspired by foursquare's ImageSlideshow, the user may scroll through images loaded from the Web
Stars: ✭ 324 (-7.69%)
Mutual labels:  hacktoberfest, image
Exodus
Platform to audit trackers used by Android application
Stars: ✭ 349 (-0.57%)
Mutual labels:  hacktoberfest
Itop
A simple, web based IT Service Management tool
Stars: ✭ 349 (-0.57%)
Mutual labels:  hacktoberfest
Awesome Hacktoberfest 2020
A curated list of awesome Hacktoberfest 2020 repositories, guides and resources
Stars: ✭ 349 (-0.57%)
Mutual labels:  hacktoberfest
Datawave
DataWave is an ingest/query framework that leverages Apache Accumulo to provide fast, secure data access.
Stars: ✭ 347 (-1.14%)
Mutual labels:  hacktoberfest
Rethinkdb.driver
🎧 A NoSQL C#/.NET RethinkDB database driver with 100% ReQL API coverage.
Stars: ✭ 350 (-0.28%)
Mutual labels:  hacktoberfest
File Upload With Preview
🖼 A simple file-upload utility that shows a preview of the uploaded image. Written in pure JavaScript. No dependencies. Works well with Bootstrap 4 or without a framework.
Stars: ✭ 352 (+0.28%)
Mutual labels:  image
Larasail
LaraSail - Set Sail with your Laravel app on DigitalOcean
Stars: ✭ 348 (-0.85%)
Mutual labels:  hacktoberfest
Bulletproof
PHP secure Image uploader, with a nice API
Stars: ✭ 352 (+0.28%)
Mutual labels:  image
Expressa
API creation middleware with an admin interface
Stars: ✭ 347 (-1.14%)
Mutual labels:  hacktoberfest
Animatify Ios
Animation, Effects & Transitions for iOS
Stars: ✭ 350 (-0.28%)
Mutual labels:  hacktoberfest
Meteor Autocomplete
Client/server autocompletion designed for Meteor's collections and reactivity.
Stars: ✭ 352 (+0.28%)
Mutual labels:  hacktoberfest
Askql
AskQL is a query language that can express any data request
Stars: ✭ 352 (+0.28%)
Mutual labels:  hacktoberfest
Sharpen
Visual Studio extension that intelligently introduces new C# features into your existing codebase
Stars: ✭ 351 (+0%)
Mutual labels:  hacktoberfest

Welcome to node-html-to-image 🌄

Version Documentation License: Apache--2.0 Twitter: yvonnickfrin

A Node.js library that generates images from HTML

🏠 Homepage

Description

This module exposes a function that generates images (png, jpeg) from HTML. It uses puppeteer in headless mode to achieve it. Additionally, it embarks Handlebars to provide a way to add logic in your HTML.

Install

npm install node-html-to-image
# or
yarn add node-html-to-image

Usage

Simple example

const nodeHtmlToImage = require('node-html-to-image')

nodeHtmlToImage({
  output: './image.png',
  html: '<html><body>Hello world!</body></html>'
})
  .then(() => console.log('The image was created successfully!'))

TypeScript support

Types are included in the package. Enable the esModuleInterop compiler flag then change all references of require with import statements and you should be good to go:

import nodeHtmlToImage from 'node-html-to-image'

Options

List of all available options:

option description type required
output The ouput path for generated image string optional
html The html used to generate image content string required
type The type of the generated image jpeg or png (default: png) optional
quality The quality of the generated image (only applicable to jpg) number (default: 80) optional
content If provided html property is considered an handlebars template and use content value to fill it object or Array optional
waitUntil Define when to consider markup succeded. Learn more. string or Array (default: networkidle0) optional
puppeteerArgs The puppeteerArgs property let you pass down custom configuration to puppeteer. Learn more. object optional
beforeScreenshot An async function that will execute just before screenshot is taken. Gives access to puppeteer page element. Function optional
transparent The transparent property lets you generate images with transparent background (for png type). boolean optional
encoding The encoding property of the image. Options are binary (default) or base64. string optional

Setting output image resolution

node-html-to-image takes a screenshot of the body tag's content. If you want to set output image's resolution you need to set its dimension using CSS like in the following example.

const nodeHtmlToImage = require('node-html-to-image')

nodeHtmlToImage({
  output: './image.png',
  html: `<html>
    <head>
      <style>
        body {
          width: 2480px;
          height: 3508px;
        }
      </style>
    </head>
    <body>Hello world!</body>
  </html>
  `
})
  .then(() => console.log('The image was created successfully!'))

Example with Handlebars

Handlerbars is a templating language. It generates HTML from a template and an input object. In the following example we provide a template to node-html-to-image and a content object to fill the template.

const nodeHtmlToImage = require('node-html-to-image')

nodeHtmlToImage({
  output: './image.png',
  html: '<html><body>Hello {{name}}!</body></html>',
  content: { name: 'you' }
})
  .then(() => console.log('The image was created successfully!'))

Handlebars provides a lot of expressions to handle common use cases like conditions or loops.

Dealing with images

If you want to display an image which is stored remotely do it as usual. In case your image is stored locally I recommend having your image in base64. Then you need to pass it to the template with the content property. Here is an example:

const nodeHtmlToImage = require('node-html-to-image')
const fs = require('fs');

const image = fs.readFileSync('./image.jpg');
const base64Image = new Buffer.from(image).toString('base64');
const dataURI = 'data:image/jpeg;base64,' + base64Image

nodeHtmlToImage({
  output: './image.png',
  html: '<html><body><img src="{{imageSource}}" /></body></html>',
  content: { imageSource: dataURI }
})

Using the buffer instead of saving to disk

If you don't want to save the image to disk and would rather do something with it immediately, you can use the returned value instead! The example below shows how you can generate an image and send it back to a client via using express.

const express = require('express');
const router = express.Router();
const nodeHtmlToImage = require('node-html-to-image');

router.get(`/api/tweet/render`, async function(req, res) {
  const image = await nodeHtmlToImage({
    html: '<html><body><div>Check out what I just did! #cool</div></body></html>'
  });
  res.writeHead(200, { 'Content-Type': 'image/png' });
  res.end(image, 'binary');
});

Generating multiple images

If you want to generate multiple images in one call you must provide an array to the content property.

Saving to disk

To save on the disk you must provide the output property on each object in the content property.

nodeHtmlToImage({
  html: '<html><body>Hello {{name}}!</body></html>',
  content: [{ name: 'Pierre', output: './image1.png' }, { name: 'Paul', output: './image2.png' }, { name: 'Jacques', output: './image3.png' }]
})
  .then(() => console.log('The images were created successfully!'))

Using buffers

If you don't want to save the images to disk you can use the returned value instead. It returns an array of Buffer objects.

const images = await nodeHtmlToImage({
  html: '<html><body>Hello {{name}}!</body></html>',
  content: [{ name: 'Pierre' }, { name: 'Paul' }, { name: 'Jacques' }]
})

Related

Libraries

Articles

Run tests

yarn test

Author

👤 FRIN Yvonnick [email protected]

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 FRIN Yvonnick [email protected].
This project is Apache--2.0 licensed.


This README was generated with ❤️ by readme-md-generator

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].