Tools to Capture and Convert the Web

Convert webpages and HTML to PDF

PHP API

GrabzIt's PHP API provides the following features to help integrate GrabzIt into your app as easily as possible when converting webpages or HTML straight to PDF documents. However before you start remember that after calling the URLToPDF, HTMLToPDF or FileToPDF methods the Save or SaveTo method must be called to actually create the PDF.

Basic Options

Capturing webpages as PDF converts the entire web page into a PDF document that can consist of many pages. Only one parameter is required in order to convert a web page into a PDF document or to convert HTML to PDF as shown in the below examples.

$grabzIt->URLToPDF("https://www.tesla.com");
//Then call the Save or SaveTo method
$grabzIt->HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>");
//Then call the Save or SaveTo method
$grabzIt->FileToPDF("example.html");
//Then call the Save or SaveTo method

Custom Identifier

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

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setCustomId(123456);

$grabzIt->URLToPDF("https://www.tesla.com", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setCustomId(123456);

$grabzIt->HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setCustomId(123456);

$grabzIt->FileToPDF("example.html", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");

Headers and Footers

When you create a PDF 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.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setMarginTop(20);
$options->setMarginBottom(20);
$options->setTemplateId("my template");

$grabzIt->URLToPDF("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.pdf");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setMarginTop(20);
$options->setMarginBottom(20);
$options->setTemplateId("my template");

$grabzIt->HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.pdf");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setMarginTop(20);
$options->setMarginBottom(20);
$options->setTemplateId("my template");

$grabzIt->FileToPDF("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.pdf");

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 PHP library. You must pass the CSS selector of the HTML element you wish to convert to the setTargetElement method.

...
<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.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItPDFOptions();
$options->setTargetElement("#Article");

$grabzIt->URLToPDF("http://www.bbc.co.uk/news", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.pdf");

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