【问题标题】:Generating web page into image is not working for Spatie BrowsershotSpatie Browsershot 无法将网页生成为图像
【发布时间】:2019-09-20 15:24:39
【问题描述】:

我正在使用 Laravel 框架开发一个 Web 应用程序。我需要在我的应用程序中将网页生成为 pdf/ 图像。我正在使用 PHP 浏览器拍摄 spatie 包,https://github.com/spatie/browsershot。我使用以下代码创建了一个测试函数来生成图像。

\Spatie\Browsershot\Browsershot::url("https://www.google.com/")->save(public_path() . '/example.png');

当我运行该函数时,出现以下错误。

The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node '/var/www/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"https:\/\/www.google.com\/","action":"screenshot","options":{"type":"png","path":"\/var\/www\/public\/example.png","args":[],"viewport":{"width":800,"height":600}}}'" failed. Exit Code: 127(Command not found) Working directory: /var/www/public Output: ================ Error Output: ================ sh: 1: npm: not found sh: 1: node: not found

我正在为我的项目使用 Docker 和 Docker Compose。我的代码有什么问题?

【问题讨论】:

  • 它说 npm 和 node 没有找到,那么你需要安装那些
  • 我的机器上有它们
  • 你的 Docker 镜像中可能没有安装 NPM 和 Node。尝试使用基础映像(Apt、Yum、Apk 等)的安装工具将它们添加到您的 Dockerfile
  • 您好,可以举个例子吗?

标签: php laravel browsershot


【解决方案1】:

你需要告诉 BrowserShot 指向 node 和 npm 的安装二进制文件。
用这种方式修改你的代码

\Spatie\Browsershot\Browsershot::url("https://www.google.com/")
    ->setNodeBinary('/usr/bin/node')
    ->setNpmBinary('/usr/bin/npm')
    ->save(public_path() . '/example.png');

确保相应地修改“/usr/bin/node”和“/usr/bin/npm”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 2020-05-24
    • 2018-07-01
    • 1970-01-01
    • 2011-03-20
    • 2018-08-05
    • 2021-06-14
    • 2011-05-23
    相关资源
    最近更新 更多