PhantomJs: Convert a String to an Image/PDF.

To convert a string or HTML to PNG or PDF using phantomjs:

Create a File say texttoimage.js

Add this code:

var page = require(‘webpage’).create();
page.viewportSize = { width: 200, height : 200 };
page.content =  “This Text will be converted to Image”;
window.setTimeout(function () {
}, 1000);

Save and Run the Script from console: phantomjs texttoimage.js

A image named newimage.png will be generated on same directory where your script is saved.

Here is the output:

PhantomJS : Converted Text to Image