捕获和转换Web的工具

将URL和HTML转换为DOCX

PHP API

添加了转换HTML或网页的功能 into使用您的应用程序将Word文档轻松转换为 GrabzIt的PHP API。 但是,在开始之前,请记住 URLToDOCX, HTMLToDOCX or FileToDOCX 方法。 的 Save or SaveTo 必须调用方法才能实际创建DOCX。

基本选项

在DOCX转换整个网页时捕获网页 int可以包含许多页面的Word文档。 在以下示例中,PHP 将HTML转换为DOCX 和一个网页 intoa Word文档,只有一个必需的参数。

$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

自订识别码

您可以将自定义标识符传递给 docx文档 方法,如下所示,然后将该值返回到您的GrabzIt PHP处理程序。 例如,此自定义标识符可以是数据库标识符,从而允许DOCX文档与特定数据库记录相关联。

$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");

页眉和页脚

虽然GrabzIt不支持传统的Word模板。 在Word文档中添加页眉或页脚时,您可以要求您应用 模板 生成的DOCX。 该模板必须是 saved并会指定页眉和页脚的内容以及任何特殊变量。 在下面的示例代码中,用户正在使用他们创建的名为“我的模板”的模板。

$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");

将HTML元素转换为DOCX

如果只想直接转换div或span等HTML元素 int您可以使用GrabzIt的PHP库获得Word文档。 您必须通过 CSS选择器 您希望转换为HTML元素的 setTargetElement GrabzIt方法DOCXOptions 类。

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

在此示例中,我们希望捕获跨度中ID为的所有内容 Article。 将其传递给GrabzIt,如下所示。

$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");

如示例中所示将URL转换为Word或将HTML转换为Word都没有关系。 两者都以完全相同的方式定位HTML元素。