放置网站屏幕截图等的最佳方法 into您的Node.js应用程序通过执行以下步骤:
这个图书馆是 仅服务器端。 如果您想使用客户端JavaScript处理捕获,则应使用我们的 客户端JavaScript API 代替。
要使用API,您首先需要创建 客户 对象,传递您的 应用程序密钥和应用程序秘密 从您的GrabzIt帐户到构造函数。
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
然后使用其中之一 图片, PDF, 动画 or 表 捕获方法 网址 or 转换HTML.
// To take a image screenshot client.url_to_image("https://www.tesla.com"); // Or to take a PDF screenshot client.url_to_pdf("https://www.tesla.com"); // Or to convert online videos into animated GIF's client.url_to_animation("http://www.example.com/video.avi"); // Or to capture table(s) client.url_to_table("https://www.tesla.com");
// To convert HTML into a image client.html_to_image("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a PDF document client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a CSV, XLSX or JSON document client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>");
// To convert HTML file into a image client.file_to_image("example.html"); // Or convert HTML file into a PDF document client.file_to_pdf("example.html"); // Or convert HTML file into a CSV, XLSX or JSON document client.file_to_table("tables.html");
接下来必须是捕获 saved,为此,您必须使用 save
or save_to
方法,两者都传入一个函数来处理已发生的任何错误。 的 save 下面的方法调用包含 处理器 这将处理 回电话 来自GrabzIt和 save 屏幕截图或截图。
client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
另一个选择是使用 同步的 save_to 方法,这将迫使您的应用程序在屏幕截图创建时等待,因此仅在 save 方法无法使用。
截图一旦创建,它将自动 saved到提供的文件路径。 请注意,Node.js库仅在服务器端可用,因为它需要服务器功能才能正常工作。
client.save_to("images/result.jpg", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
这只是GrabzIt的Node.js API的概述,在创建屏幕快照,动画GIF或转换HTML时,还有更多选项,包括: 配置浏览器高度,浏览器宽度,屏幕截图高度,屏幕截图宽度,拍摄屏幕截图之前的延迟以及屏幕截图的图像格式。 对于所有这些选项以及转换网络的能力 into PDF,CSV或动画GIF,请参阅 客户 文档。