Tools to Capture and Convert the Web

How to convert HTML to Word in PHP

PHP API

You can now easily add a feature to convert HTML content or web pages into Word documents to your app, using GrabzIt's PHP API. However before you start remember that after calling the URLToDOCX, HTMLToDOCX or FileToDOCX methods. You must call the Save or SaveTo method to actually create the DOCX.

Basic Options

Saving web pages as DOCX turns the whole page into a Word document with multiple pages. In the below code examples it shows how to convert HTML to Word in PHP and a web page into DOCX. With only one required parameter.

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

Custom Identifier

You can pass a custom identifier to the DOCX methods as shown below. Your GrabzIt PHP handler then receives this value, when the DOCX is complete. For example, this custom identifier could serve as a database identifier, enabling the association of a DOCX document with a specific database record.

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

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

$grabzIt->URLToDOCX("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\GrabzItDOCXOptions();
$options->setCustomId(123456);

$grabzIt->HTMLToDOCX("<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\GrabzItDOCXOptions();
$options->setCustomId(123456);

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

Headers and Footers

While GrabzIt doesn't support the traditional Word Template. When you add headers or footers to a Word document, you can specify that you want to apply a template to the generated DOCX.

You must save this template in advance. The template will specify the contents of the header and footer along with any special variables. In the example code below the user is using a template they created called "my template".

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

$options = new \GrabzIt\GrabzItDOCXOptions();
$options->setTemplateId("my template");

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

$options = new \GrabzIt\GrabzItDOCXOptions();
$options->setTemplateId("my template");

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

$options = new \GrabzIt\GrabzItDOCXOptions();
$options->setTemplateId("my template");

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

Convert HTML element to DOCX

You can change an HTML element, like a div or span, into a Word document with GrabzIt's PHP library. To convert an HTML element, pass its CSS selector to the setTargetElement method in the GrabzItDOCXOptions class.

...
<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. You can pass this information 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\GrabzItDOCXOptions();
$options->setTargetElement("#Article");

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

It doesn’t matter if you are converting a URL to Word as shown in the example or HTML to Word. Both target HTML elements in exactly the same way.