phantomjs插件---实现通过链接生成网页截图

Phantomjs | PhantomJS


  • 配置要求

    windows下,安装完成phantomJS
    设置phantomjs环境变量【也可直接使用phantomjs目录下的执行文件】
    直接通过访问php文件执行/通过cmd命令行执行【phantomjs phantom_script.js】

    linux下,安装完成phantomJS
    设置phantomjs环境变量
    直接命令行执行phantom_script.js即可【phantomjs phantom_script.js】

  • phantom.php

php 复制代码
<?php
// 引入并执行 PhantomJS 脚本
$phantomScript = './phantom_script.js';
$command = 'phantomjs ' . $phantomScript;
$output = shell_exec($command);

echo 'Screenshot saved at: ' . trim($output);
  • phantom_script.js
javascript 复制代码
var page = require('webpage').create();
page.viewportSize = { width: 900, height: 600 };

var url = 'https://www.kancloud.cn/manual/thinkphp6_0/1037479';  // 替换为您的URL

var timestamp = new Date().getTime();
var savePath = './phantomimg/'+timestamp+'.png';  // 保存截图的路径

function takeScreenshot() {
    page.open(url, function(status) {
        if (status === 'success') {
            // 在页面加载完成后,等待一段时间以确保动态数据(如echarts 图表)渲染完全
            setTimeout(function() {
                page.render(savePath);
                console.log(savePath);
                phantom.exit();
            }, 2000);  // 设置等待时间,单位为毫秒(根据网页数据加载时间参考等待时间)
        } else {
            console.log('Unable to load the page.');
            phantom.exit();
        }
    });
}

takeScreenshot()
相关推荐
winfredzhang3 天前
从零构建:手写一个支持“高度定制化排版”的 Chrome 网页摘录插件
chrome·pdf·插件·epub·零碎信息归档
liuzhilongDBA5 天前
论文精读|插件无政府状态
插件·兼容性·postgres
随风飘摇的土木狗12 天前
【MATLAB第121期】基于MATLAB的sobol、lhs等17种方法数据抽样插件(含UI界面)
matlab·插件·采样·lhs·数据抽样
梁萌19 天前
idea使用AI插件(CodeGeeX)
java·ide·ai·intellij-idea·插件·codegeex
l1t25 天前
PostgreSQL pg_clickhouse插件的安装和使用
数据库·clickhouse·postgresql·插件
狼性书生1 个月前
uniapp实现的时间范围选择器组件
前端·uni-app·vue·组件·插件
G皮T1 个月前
【Elasticsearch】大慢查询隔离(二):选择插件
大数据·elasticsearch·搜索引擎·全文检索·插件·性能·查询
sean9081 个月前
Firefox 安装非市场的插件
firefox·插件·市场
wtsolutions1 个月前
WPS另存为JSON,WPS导出JSON, WPS表格转换成JSON : Excel to JSON WPS插件使用指南
json·excel·wps·插件·加载项·wtsolutions