【发布时间】:2016-08-03 12:35:58
【问题描述】:
我正在尝试使用Phantomjs 从用户所在的同一页面捕获屏幕截图。
例如,一个用户在 my-page.html 上并且对该页面的元素进行了一些更改,现在我需要对该页面中的一个元素(DIV)进行截图(my-page.html) 并保存。
我发现了一些Phantomjs and php 的示例,我在我的服务器上进行了测试和工作,它也将图像存储在我的服务器上,但我发现的所有示例都是用于截取外部页面/URL 的屏幕截图,而不是“当前页”。
这在 Html2canvas 中是一个相当简单的过程,但生成的图像质量一点也不好,所以我决定使用 Phantomjs 生成更高质量的屏幕截图,并且它还允许我放大页面。
这是一个使用 Phantomjs 截取外部 URL 的简单示例:
var system = require("system");
if (system.args.length > 0) {
var page = require('webpage').create();
page.open(system.args[1], function() {
//viewportSize being the actual size of the headless browser
page.viewportSize = { width: 3000, height: 3000 };
//the clipRect is the portion of the page you are taking a screenshot of
page.clipRect = { top: 0, left: 0, width: 3000, height: 3000 };
page.zoomFactor = 300.0/72.0;
var pageTitle = system.args[1].replace(/http.*\/\//g, "").replace("www.", "").split("/")[0]
var filePath = "img/" + pageTitle + '.png';
page.render(filePath);
console.log(filePath);
phantom.exit();
});
}
有人可以告诉我这是否可能吗?
编辑(回答我自己的问题),
事实证明,如果页面的元素已被用户实时编辑,则您无法截取当前页面的屏幕截图。您可以使用 phantomjs 截取的唯一屏幕截图是页面的基本内容。
原因:phantomjs 是一个无头浏览器,使用在服务器上运行的 QtWebKit,它不是一个与 html2canvas 相同的 javascript 库。
别人解释和体验过HERE:
另一个对我正在处理的项目来说是个问题的用例是您需要拖放。无头驱动程序具有一些基本功能,但如果您需要能够设置精确坐标,则只能使用 Selenium。
【问题讨论】:
标签: javascript php jquery html