捕获和转换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 使用这些技术进行控制.