0

I am developing a web application using the Laravel framework. I need to generate the web page into pdf/ image in my application. I am using PHP browser shot spatie package for it, https://github.com/spatie/browsershot. I created a test function to generate the image using it with the following code.

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

When I run the function, I got the following error.

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

I am using Docker and Docker Compose for my project. What is wrong with my code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

1 Answers1

0

You need to tell BrowserShot to point to the installation binary of node and npm.
Modify your code this way

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

Make sure to modify '/usr/bin/node' and '/usr/bin/npm' accordingly.

Weybansky
  • 586
  • 4
  • 7
  • " Make sure to modify '/usr/bin/node' and '/usr/bin/npm' accordingly. " , which path needs to pass here?? – Sho Dec 29 '22 at 12:23