0

I have a Laravel project which attempts to execute this command from within a controller.

$filename = 'google.png';
$screenshot = Browsershot::url('http://google.com/')
    ->setNodeBinary('C:\PROGRA~1\nodejs\node.exe')
    ->setNodeModulePath('D:\Work\Project\node_modules')
    ->setOption('landscape', true)
    ->windowSize(3840, 2160)
    ->waitUntilNetworkIdle()
    ->save($filename);

When I serve the Laravel project (php artisan serve) the command Browsershot invokes with Process results in an error.

The process:

$ "C:\PROGRA~1\nodejs\node.exe" "D:\Work\Project\vendor\spatie\browsershot\src/../bin/browser.js" "{\"url\":\"http:\/\/google.com\",\"action\":\"screenshot\",\"options\":{\"type\":\"png\",\"path\":\"google.jpg\",\"args\":[\"--no-sandbox\"],\"viewport\":{\"width\":1920,\"height\":600},\"timeout\":120000,\"fullPage\":true}}"

Error

node:events:491 throw er; // Unhandled 'error' event ^

Error: read ENOTCONN at tryReadStart (node:net:638:20) at Socket._read (node:net:649:5) at Socket.Readable.read (node:internal/streams/readable:487:10) at Socket.read (node:net:705:39) at new Socket (node:net:425:12) at Object.Socket (node:net:295:41) at createSocket (node:internal/child_process:328:14) at ChildProcess.spawn (node:internal/child_process:445:23) at Object.spawn (node:child_process:713:9) at BrowserRunner.start (D:\Work\Project\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:121:34) Emitted 'error' event on Interface instance at: at Socket.onerror (node:readline:268:10) at Socket.emit (node:events:513:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) { errno: -4053, code: 'ENOTCONN', syscall: 'read' }

If I run exactly that command from the command line, it works. If I take exactly that PHP block and put it in a normal php file and call with Apache, from the same directory with the same Composer vendor directory, it works:

test.php

    $filename = 'google.png';
    $screenshot = Browsershot::url('http://google.com/')
        ->setNodeBinary('C:\PROGRA~1\nodejs\node.exe')
        ->setNodeModulePath('D:\Work\Project\node_modules')
        ->setOption('landscape', true)
        ->windowSize(3840, 2160)
        ->waitUntilNetworkIdle()
        ->save($filename);

So it seems like a problem not with Browsershot or Puppeteer directly but some combination of them and Laravel / the Laravel server. Does anyone have any ideas?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mat
  • 11
  • 2
  • In the end I bypassed this issue by just using Apache instead of Laravel's Artisan server. The code exectued first time serving it in this way. – Mat Oct 04 '22 at 10:01

0 Answers0