Tools to Capture and Convert the Web

Convert webpages and HTML to PDF

Node.js API

When converting webpages and HTML to PDF GrabzIt's Node.js API provides the following features that help integrate GrabzIt into your system as easily as possible. However before you start remember that after calling the url_to_pdf, html_to_pdf or file_to_pdf methods the save or save_to method must be called to take the PDF screenshot or to convert HTML straight to PDF.

Basic Options

Only one parameter is required in order to convert a web page into a PDF document or to convert HTML to PDF as shown below.

client.url_to_pdf("https://www.tesla.com");
//Then call the save or save_to method
client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>");
//Then call the save or save_to method
client.file_to_pdf("example.html");
//Then call the save or save_to method

Custom Identifier

You can pass a custom identifier to the PDF methods as shown below, this value is then returned to your GrabzIt Node.js handler. For instance this custom identifier could be a database identifier, allowing a screenshot to be associated with a particular database record.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.url_to_pdf("https://www.tesla.com", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.file_to_pdf("example.html", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});

Headers and Footers

When you create a PDF screenshot you can request that you want to apply a particular template to the generated PDF. This template must be saved in advance and will specify the contents of the header and footer along with any special variables. In the example code below the user is using their template called "my template".

If there is not a large enough top or bottom margin for the header or footer respectively, it will not appear in the PDF. In the below example we have set the top and bottom margins to 20 to provide plenty of space.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"};

client.url_to_pdf("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.pdf", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"};

client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the save or save_to method
client.save_to("result.pdf", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"};

client.file_to_pdf("example.html", options);
//Then call the save or save_to method
client.save_to("result.pdf", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

Convert HTML element to PDF

If you want to just convert a HTML element such as a div or span directly into a PDF document you can with GrabzIt's Node.js library. You must pass the CSS selector of the HTML element you wish to convert to the targetElement parameter.

...
<span id="Article">
<p>This is the content I am interested in.</p>
<img src="myimage.jpg">
</span>
...

In this example, we wish to capture all the content in the span which has the id of Article, therefore we pass this to GrabzIt as shown below.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

grabzIt.url_to_pdf("http://www.bbc.co.uk/news", {"targetElement":"#Article"});
//Then call the save or save_to method
client.save_to("result.pdf", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

How a PDF is cropped when targeting a HTML element can be controlled using these techniques.