捕獲和轉換Web的工具

將網頁和HTML轉換為PDF

PHP API

GrabzIt的PHP API 提供以下功能來幫助 integrate GrabzIt into將網頁或HTML直接轉換為PDF文檔時,盡可能輕鬆地使用您的應用程序。 但是,在開始之前,請記住 URLToPDF, HTMLToPDF or FileToPDF 方法 Save or SaveTo 必須調用方法來實際創建PDF。

基本選項

以PDF格式捕獲網頁會轉換整個網頁 int可以包含許多頁面的PDF文檔。 只需一個參數即可轉換網頁 intpdf文檔或 將HTML轉換為PDF 如以下示例所示。

$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

自訂識別碼

您可以將自定義標識符傳遞給 PDF 方法,如下所示,然後將該值返回到您的GrabzIt PHP處理程序。 例如,該自定義標識符可以是數據庫標識符,從而允許將屏幕截圖或PDF與特定的數據庫記錄相關聯。

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

頁眉和頁腳

創建PDF時,您可以請求要應用特定的PDF。 模板 到生成的PDF。 該模板必須是 saved並會指定頁眉和頁腳的內容以及任何特殊變量。 在下面的示例代碼中,用戶正在使用其名為“我的模板”的模板。

如果頁眉或頁腳分別沒有足夠大的頂部或底部邊距,則它將不會出現在PDF中。 在下面的示例中,我們將頂部和底部邊距設置為20以提供足夠的空間。

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

將HTML元素轉換為PDF

如果只想直接轉換div或span等HTML元素 int您可以使用GrabzIt的PHP庫獲得PDF文檔。 您必須通過 CSS選擇器 您希望轉換為HTML元素的 setTargetElement 方法。

...
<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\GrabzItPDFOptions();
$options->setTargetElement("#Article");

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

定位HTML元素時如何裁剪PDF 使用這些技術進行控制.