I am trying to make Spatie Browsershot work on Windows with XAMPP.
First of all I installed puppeteer:
npm install -g puppeteer
At first I wanted to output the result directly to the browser, so I had the following code in the controller:
use Spatie\Browsershot\Browsershot;
public function createPDFWithBrowsershot(Request $request)
{
Browsershot::html('<h1>Test</h1>')->pdf();
}
But then I get the following error:
The command "node ^"C:^\xampp^\htdocs^\my_project^\vendor^\spatie^\browsershot^\src/../bin/browser.js^ ..."
So then I tried to do the following (though not sure it's a good option for Windows, but it did make the above error disappear):
public function createPDFWithBrowsershot(Request $request)
{
Browsershot::html('<h1>Test</h1>')->setNodeBinary('PATH %~dp0;%PATH%;')->pdf();
}
And the error above disappeared, however the page was blank.
So instead of trying to output the PDF to the browser, I tried to save it as a pdf file:
public function createPDFWithBrowsershot(Request $request)
{
$save_to_file = 'C:/pdfs/file.pdf';
Browsershot::html('<h1>Test</h1>')->setNodeBinary('PATH %~dp0;%PATH%;')->savePdf($save_to_file);
}
But I got a new error:
For some reason Chrome did not write a file at
C:/pdfs/file.pdf. Command ======= [] Output ======
I also tried several other paths:
$save_to_file = 'C:\pdfs\file.pdf';
or
$save_to_file = storage_path('pdfs/file.pdf');
But I get the same error.
I even tried uninstalling the global puppeteer and installing it only in my project:
npm install puppeteer
And even installing chromium:
npm install chromium
Nothing makes it work
Why?
It's sometimes hard to config Spatie BrowserShot, harder to send it in production (you will have to setup you prod environment and it can be a pain). Here is the config i use locally on my windows 10, hope it will help you :
My Spatie BrowserShot version in composer.json
My puppeteer version in package.json
The controller which recieve the Illuminate\Http\Request from frontend
The template you use has to hide some elements in order to be "printable", here an exemple
EDIT in production, you will have to set the paths for the chrome standalone and the node modules, here is mine in linux machine