Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To execute Puppeteer from a Laravel Controller using JavaScript code, follow these steps:

  1. Install Puppeteer package by running the following command in your Laravel project directory:
npm install puppeteer
  1. Create a new JavaScript file (e.g. puppeteer_script.js) in your project's public folder.

  2. Add the following code to your new script file:

const puppeteer = require('puppeteer');

(async() => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
    await page.goto('https://example.com');
    //perform operations here
  await browser.close();
})();
  1. In your Laravel Controller's method, add the following line to execute the puppeteer_script.js file:
$cmd = 'node ' . public_path('puppeteer_script.js');
$output = shell_exec($cmd);

This will execute the puppeteer_script.js file in a Node process, running the Puppeteer code and returning any output to your Laravel Controller.

Note: Make sure that you have Node.js installed on your server for this to work.