Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

donatello's avatar

Errors with puppeteer while setting up Browsershot for blade->PDF download

0

I have a laravel 10 app. In my index.blade.php I have list of workers, each of whom has a separate view page, in index.blade.php I have a print button, which on click should print the pdf version of the separate blade view page of the worker. Since dompdf doesn't support Arabic, flexbox etc. I thought of using browsershot by spatie.

I followed instructions as per documenation here: https://github.com/spatie/browsershot

And sadly what could've been so simple has made me crazy with one issue after another.

All seem to be related to puppeteer.

My latest error message is:

Symfony  \  Component  \  Process  \  Exception  \  ProcessFailedException PHP 8.2.0 10.24.0 The command ""C:\Program Files\nodejs\node.exe" "C:\xampp\htdocs\appname\appname\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""http://localhost:8000/admin/workers/3"",""action"":""pdf"",""options"":{""args"":[],""viewport"":{""width"":800,""height"":600},""newHeadless"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\xampp\htdocs\appname\appname\public Output: ================ Error Output: ================ TimeoutError: Navigation timeout of 30000 ms exceeded at Timeout. (C:\xampp\htdocs\boom\boom\node_modules\puppeteer-core\lib\cjs\puppeteer\util\Deferred.js:27:33) at listOnTimeout (node:internal/timers:564:17) at process.processTimers (node:internal/timers:507:7) ERROR: The process "40040" not found. Kindly not the process number is different each time.

Before that, I had another error:

Error: ENOENT: no such file or directory, mkdtemp 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX'] { errno: -4058, code: 'ENOENT', syscall: 'mkdtemp', path: 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX' } I'm on Windows 10, [email protected], node v18.13.0,npm 8.19.3.

I don't understand what's causing this, can't find anything on other stackoverflow or laracast answer.

Please help.

My controller code:

public function generatePdfPreview($id) { $worker = Worker::findOrFail($id);

    // Use Browsershot to capture the web page and save it as a PDF
    $pdf = Browsershot::url(route('admin.workers.show', $worker->id))
        ->setNodeBinary("C:\Program Files\nodejs\node.exe")
        ->setOption('newHeadless', true)
        ->pdf();


    return response($pdf)->header('Content-Type', 'application/pdf');
}
0 likes
7 replies
jbloomstrom's avatar

@sabira_khan When using double-quotes for strings in PHP, special characters like \ need to be escaped with an additional slash (i.e. \\) - in this string "C:\Program Files\nodejs\node.exe" it is interpreting the \n characters as linebreaks.

 C:\Program Files
  odejs
  ode.exe

Try this:

$pdf = Browsershot::url(route('admin.workers.show', $worker->id))
    ->setNodeBinary("C:\\Program Files\\nodejs\\node.exe")
    ->timeout(60000) // Increase timeout to 60 seconds
    ->setOption('newHeadless', true)
    ->pdf();
razan-rai's avatar

Sometime if you have image then you need to convert the image to base64.

Converting to base64

public function generateBase64EncodedImage($filename){
        $filePath = public_path($filename);
        if (file_exists($filePath)) {
            return base64_encode(file_get_contents($filePath));
        }
        return null;
    }

Generation of base64 $school->logo = $this->generateBase64EncodedImage($school->logo);

Using base64 in blade <img class="gradesheet_logo" src="data:image/jpeg;base64,{{ $school->logo }}" alt="School Logo">

Please or to participate in this conversation.