Node.js Convert SVG to PNG,JPEG,TIFF and HEIF Format Using Sharp Library in Javascript Full Tutorial For Beginners

Node.js Convert SVG to PNG,JPEG,TIFF and HEIF Format Using Sharp Library in Javascript Full Tutorial For Beginners

 

Welcome folks today in this post we will be converting svg to png,jpeg,tiff and heif formats using sharp library in javascript. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started we need to install sharp library using the npm command as shown below

 

npm i sharp

 

And after installing this we need to create an index.js file and copy paste the following code

 

index.js

 

 

SVG TO PNG

 

 

const sharp = require("sharp")

sharp("image.svg")
  .png()
  .toFile("image.png")
  .then(function(info) {
    console.log(info)
  })
  .catch(function(err) {
    console.log(err)
  })

 

 

And here we are converting svg to png we are using the png() method to convert this image from svg to png.

 

 

SVG TO JPEG

 

 

const sharp = require("sharp")

sharp("image.svg")
  .png()
  .toFile("image.jpg")
  .then(function(info) {
    console.log(info)
  })
  .catch(function(err) {
    console.log(err)
  })

 

See also  Build a Math Scientific Calculator in HTML CSS and Javascript Full Project For Beginners

 

 

SVG TO TIFF

 

 

const sharp = require("sharp")

sharp("image.svg")
  .png()
  .toFile("image.tiff")
  .then(function(info) {
    console.log(info)
  })
  .catch(function(err) {
    console.log(err)
  })

 

 

 

SVG TO HEIF

 

 

const sharp = require("sharp")

sharp("image.svg")
  .png()
  .toFile("image.heif")
  .then(function(info) {
    console.log(info)
  })
  .catch(function(err) {
    console.log(err)
  })

 

 

Leave a Reply