1. Products
  2.   Image
  3.   Node.js
  4.   Node-HTML-to-Image
 
  

Free Node.js Imaging API to Convert HTML to Image

Open Source Node.js Image Processing Library Fully that Support Image Loading and Conversion, Developers can Convert HTML Content into Images (JPEG, PNG, GIF) inside Node.js Apps.

In the rapidly changing world of web development, the ability to convert HTML into images opens up a world of creative possibilities. Whether it's generating social media graphics, creating visual reports, or designing custom content for emails, converting HTML to images can streamline many processes. The Node-HTML-to-Image library is an open-source tool that enables developers to do just that, providing a straightforward way to generate images from HTML using Node.js. Software Developers can create stunning visuals, generate thumbnails, and even automate tasks that require image generation. There are several important features part of the library, such as new image generation, load HTML from external source and convert it into image, add custom CSS and JavaScript, generate multiple images at once and so on.

Node-HTML-to-Image library is incredibly easy to use and it allows software developers to load and convert HTML strings or files into high-quality images with just a couple of lines of code. Developed by Frinyvonnick, this library leverages the power of Headless Chromium through the Puppeteer library to render HTML and CSS into PNG, JPEG, or even WebP images. This allows developers to create images that capture the full visual complexity of HTML, including support for modern CSS, fonts, and even JavaScript execution. You can customize the output by specifying options such as image quality, width, and height. Moreover, users can extract images from web pages, allowing them to build powerful web scraping applications. With its flexibility, customizability, and ease of use, it's no wonder why developers are flocking to this library.

Previous Next

Getting Started with Node-HTML-to-Image

The recommend way to install Node-HTML-to-Image t is using NPM. Please use the following command for a smooth installation.

Install Node-HTML-to-Image via NPM

npm install node-html-to-image

Install Node-HTML-to-Image via Yarn

yarn add node-html-to-image

Image Generation from HTML Code inside Node.js

One of the major strengths of Node-HTML-to-Image library is its simplicity as well as can be easily integrated with any Node.js application. With just a few lines of code, software developers can start generating images from HTML content inside their own Node.js applications. This ease of use makes it accessible to developers of all skill levels. In the following example, the library takes a simple HTML string and converts it into a PNG image. The output image, image.png, is saved in the root directory.

How to Generate PNG Image from HTML inside Node.js Apps?

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

nodeHtmlToImage({
  output: './image.png',
  html: '

Hello, World!

' }) .then(() => console.log('Image created successfully!'));

Complex HTML and CSS Support

The open source Node-HTML-to-Image isn’t limited to basic HTML processing. It can handle complex HTML documents with extensive CSS styling as well as JavaScript. Whether you’re using modern CSS features like Flexbox or Grid, or embedding custom fonts, the library will accurately render your content into an image file formats like JPEG, PNG, WebP and many more. Here is an example that demonstrates how developers can generates an image from a more complex HTML layout, including styles for Flexbox and custom colors, resulting in a well-designed image.

How to Generate an Image From a More Complex HTML Code in Node.js Apps?

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

const htmlContent = `

  
    

Welcome to My Website

Building the web, one pixel at a time.

`; nodeHtmlToImage({ output: './welcome-image.png', html: htmlContent }) .then(() => console.log('Complex layout image created successfully!'));

Customizable Output Image Formats

The Node-HTML-to-Image library supports multiple output image formats, including PNG, JPEG, and WebP. This flexibility allows you to choose the best format for your specific use case, whether it’s for web performance or image quality. Here is an example that shows how users can specify the output as a JPEG image, which could be useful for scenarios where file size needs to be minimized without compromising too much on quality.

How to Specify the Output Image Format to JPEG while HTML to Image Conversion in Node.js?

nodeHtmlToImage({
  output: './output-image.jpg',
  html: '

JPEG Image Example

', type: 'jpeg' }) .then(() => console.log('JPEG image created successfully!'));

Advanced Image Rendering Support in Node.js

Node-HTML-to-Image library has provided complete support for customizing the rendering process extensively. Software developers can define viewport size, device scale factor, and even inject custom CSS and JavaScript into the HTML before rendering. The following example shows how the image is rendered with a custom viewport size of 800x600 pixels, and the quality is set to 100, ensuring the highest possible image quality.

How to Render Images with Custom Viewport Size & Quality inside Node.js Applications?

nodeHtmlToImage({
  output: './custom-image.png',
  html: '

Custom Viewport

', viewport: { width: 800, height: 600 }, quality: 100 }) .then(() => console.log('Image with custom viewport created successfully!'));
 English