The best way to put website screenshots and more into your Node.js application is by following these steps:
This library is server-side only. If you want to process captures with client-side JavaScript, you should use our client-side JavaScript API instead.
To use the API you will first need to create the Client object, passing your application key and application secret from your GrabzIt account to the constructor.
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
Then use one of the image, PDF, animation or table methods to capture a URL or convert HTML.
// To take a image screenshot client.url_to_image("https://www.tesla.com"); // Or to take a PDF screenshot client.url_to_pdf("https://www.tesla.com"); // Or to convert online videos into animated GIF's client.url_to_animation("http://www.example.com/video.avi"); // Or to capture table(s) client.url_to_table("https://www.tesla.com");
// To convert HTML into a image client.html_to_image("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a PDF document client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a CSV, XLSX or JSON document client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>");
// To convert HTML file into a image client.file_to_image("example.html"); // Or convert HTML file into a PDF document client.file_to_pdf("example.html"); // Or convert HTML file into a CSV, XLSX or JSON document client.file_to_table("tables.html");
Next the capture must be saved, to do this you must use either the save
or save_to
method, both of which pass in a function to handle any errors that have occured. The save method call below includes the URL of the handler that will process the callback from GrabzIt and save the screenshot or capture.
client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
The other option is to use the synchonous save_to method, which will force your application to wait while the screenshot is created so it should only be used were the save method cannot be used.
Once the screenshot is created it will be automatically saved to the file path provided. Note that the Node.js Library will only work on the server side as it requires server features in order to work properly.
client.save_to("images/result.jpg", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
This is only a overview of GrabzIt's Node.js API, there are many more options when creating screenshots, animated GIF's or converting HTML including; configuring the browser height, browser width, screenshot height, screenshot width, the delay before a screenshot is taken and the image format of the screenshot. For all these options and the ability to convert the web into PDF's, CSV's or Animated GIF's please see the Client documentation.