捕獲和轉換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元素。