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?