Node.JS Project to Convert HTML & CSS Template to PNG Image Using Node-HTML-To-Image Library in Command Line

You are currently viewing Node.JS Project to Convert HTML & CSS Template to PNG Image Using Node-HTML-To-Image Library in Command Line

Node.js Project to Convert HTML & CSS Template to PNG Image Using node-HTML-to-image Library in Command Line

const nodeHtmlToImage = require("node-html-to-image");
 
nodeHtmlToImage({
  output: "./image.png",
  html: `<html>
    <head>
      <style>
        body {
          width: 2480px;
          height: 3508px;
          background-color:red;
          color:yellow;
          font-size:500px;
        }
      </style>
    </head>
    <body>Hello world!</body>
  </html>
  `,
}).then(() => console.log("The image was created successfully!"));

const nodeHtmlToImage = require("node-html-to-image");
const fs = require("fs");
 
const image = fs.readFileSync("./profile.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="https://codingshiksha.com/javascript/node-js-project-to-convert-html-css-template-to-png-image-using-node-html-to-image-library-in-command-line-full-project-for-beginners/{{imageSource}}" /></body></html>',
  content: { imageSource: dataURI },
});

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!'))

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply