I'm stuck with a weird problem in my Laravel project. I'm trying to make PDFs from HTML using the spatie Browsershot package. But I keep getting this error:
Error: Can't create directory, mkdtemp 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX'
errno: -4058, code: 'ENOENT', syscall: 'mkdtemp'
Here's what I'm doing:
```php
$html = view('my-view')->render();
Browsershot::html($html)
->setNodePath('C:/NodeJS/node.exe')
->setNpmPath('C:/NodeJS/npm.cmd')
->saveAsPdf('output.pdf');
It looks like it can’t make a temp folder for Chrome. Any ideas what’s going on? Has anyone run into this before? I’m not sure how to fix it. Help would be awesome!
hey mate, had a similar issue. try setting the temp directory explicitly:
Browsershot::html($html)
->setNodePath(‘C:/NodeJS/node.exe’)
->setNpmPath(‘C:/NodeJS/npm.cmd’)
->setTempPath(‘C:/temp’)
->saveAsPdf(‘output.pdf’);
make sure C:/temp exists and has write permissions. hope this helps!
I encountered a similar issue in my project. One solution that worked for me was to set the PUPPETEER_CACHE_DIR environment variable. You can do this in your .env file:
PUPPETEER_CACHE_DIR=C:\path\to\cache\directory
Make sure the specified directory exists and has the correct permissions. Then, in your code, you can use this environment variable:
Browsershot::html($html)
->setNodePath(‘C:/NodeJS/node.exe’)
->setNpmPath(‘C:/NodeJS/npm.cmd’)
->setCustomTempPath(env(‘PUPPETEER_CACHE_DIR’))
->saveAsPdf(‘output.pdf’);
This approach helped me bypass the temp directory creation issues. If it doesn’t work, you might want to check your antivirus software, as it can sometimes interfere with Puppeteer’s operations.
I’ve dealt with this issue before, and it can be frustrating. One thing that worked for me was ensuring that Puppeteer had the correct permissions to create temporary directories. Check your system’s security settings and make sure the user running your Laravel application has full control over the temp directory.
Also, have you tried running your code with administrator privileges? Sometimes that can bypass permission issues. If that works, you’ll know it’s definitely a permissions problem.
Another potential fix is to update your Browsershot and Puppeteer packages to the latest versions. I’ve seen similar errors resolved after updates.
Lastly, if all else fails, you might want to consider using a different PDF generation library like Dompdf or TCPDF. They don’t rely on Puppeteer and might be easier to set up in your environment.