spatie / laravel-pdf

Create PDF files in Laravel apps
https://spatie.be/docs/laravel-pdf
MIT License
715 stars 54 forks source link

[Bug]: Not supported for windows #70

Closed christmex closed 9 months ago

christmex commented 9 months ago

What happened?

I want to use the laravel pdf, preview it or even download it, but I can't download or even preview the PDF, it keeps getting errors, like this

image

*Additional information Node Version: v21.6.1 Npm Version: 10.4.0

How to reproduce the bug

use Spatie\LaravelPdf\Facades\Pdf;
//....
Route::get('/print',function(){
    //already had the tes blade file inside resource/views
    Pdf::view('tes', ['invoice' => 'as'])
        ->format('a4')
        ->save('invoice.pdf');
});

I'm using this basic code just for trying but it keep getting the error

Package Version

1.1.2

PHP Version

8.2.10

Laravel Version

10.42.0

Which operating systems does with happen with?

Windows

Notes

No response

zaidpirwani commented 9 months ago

facing same issue node, v20.9.0 npm, v10.1.0 php, v8.2.4 laravel, v10.43.0 laravel-pdf, v1.1.2 Windows 11 Pro, version 23H2 PhpStorm 2023.3.3, Build #PS-233.14015.96, built on January 24, 2024

node available in windows path, accessible from cmd prompt / terminal

zaidpirwani commented 9 months ago

got a new error by adding withbrowsershot, found this by diving into the code, found it later in the docs and adding setNodeBinary with path to node return Pdf::view('pdf.purchaseOrder', [ 'record' => $record, 'record_quote' => $record->quotes->where('is_selected', true)->first(), ])->format('A4') ->withBrowsershot(function (Browsershot $browsershot) { $browsershot->setNodeBinary("C:/nodejs/node.exe"); }) ->save($record->getPRNumber() . '.pdf');

Its a start, I will explore further and report

The command ""C:/nodejs/node.exe" "C:\Users\Zaid\PhpstormProjects\LaravelHello\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""file:\/\/C:\Users\Zaid\AppData\Local\Temp\1346465607-0360706001706942517\index.html"",""action"":""pdf"",""options"":{""path"":""PR-2402-0001.pdf"",""args"":[],""viewport"":{""width"":800,""height"":600},""displayHeaderFooter"":false,""format"":""A4"",""printBackground"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\Zaid\PhpstormProjects\LaravelHello\public Output: ================ Error Output: ================  Puppeteer old Headless deprecation warning: In the near future headless: true will default to the new Headless mode for Chrome instead of the old Headless implementation. For more information, please see https://developer.chrome.com/articles/new-headless/. Consider opting in early by passing headless: "new" to puppeteer.launch() If you encounter any bugs, please report them to https://github.com/puppeteer/puppeteer/issues/new/choose. [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' }

zaidpirwani commented 9 months ago

upon further testing, it seems to concrn with puppeteer and some path related matter on windows

Browsershot::url('https://example.com') ->setNodeBinary("C:/nodejs/node.exe") ->setNpmBinary("C:/nodejs/node_modules/npm/bin") ->setNodeModulePath("C:/Users/Zaid/PhpstormProjects/LaravelHello/node_modules") ->setOption('newHeadless', true) ->save('a.png');

new error, same error shows when I use laravel-pdf, so the error is a bit more upstream... The command ""C:/nodejs/node.exe" "C:\Users\Zaid\PhpstormProjects\LaravelHello\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""https:\/\/example.com"",""action"":""screenshot"",""options"":{""type"":""png"",""path"":""a.png"",""args"":[],""viewport"":{""width"":800,""height"":600},""newHeadless"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\Zaid\PhpstormProjects\LaravelHello\public Output: ================ Error Output: ================ [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' }

though I would appreciate if any guidance and help is provided, am stuck on where to go now

zaidpirwani commented 9 months ago

another update, running simple browsershot command in the terminal works

php artisan tinker
use Spatie\Browsershot\Browsershot;
Browsershot::url('https://example.com')->setOption('newHeadless', true)->save('a.pdf');

and also the below works as well in terminal

php artisan tinker
use Spatie\LaravelPdf\Facades\Pdf;
Pdf::html('<h1>Hello world!!</h1>')->save('invoice.pdf');
zaidpirwani commented 9 months ago

after much code diving, it seemed that the process of puppeteer being run by browsershot was not getting any environment variables, I traced the process creation to line 1032 in file vendor/spatie/browsershot/src/Browsershot.php $process = $this->isWindows() ? new Process($fullCommand) : Process::fromShellCommandline($fullCommand);

and modified

$process = $this->isWindows() ? new Process($fullCommand,null,getenv() ) : Process::fromShellCommandline($fullCommand);

now I am able to generate PDF Files. I know am not supposed to modify files in vendor folders - need to check further

also am sending the whole env of laravel, not sure if that is right or not - am currently running this locally.

Mentioned this here as well: https://github.com/spatie/browsershot/discussions/728

freekmurze commented 9 months ago

This should be handled on Browsershot

adrianojsle commented 8 months ago

after much code diving, it seemed that the process of puppeteer being run by browsershot was not getting any environment variables, I traced the process creation to line 1032 in file vendor/spatie/browsershot/src/Browsershot.php $process = $this->isWindows() ? new Process($fullCommand) : Process::fromShellCommandline($fullCommand);

and modified

$process = $this->isWindows() ? new Process($fullCommand,null,getenv() ) : Process::fromShellCommandline($fullCommand);

now I am able to generate PDF Files. I know am not supposed to modify files in vendor folders - need to check further

also am sending the whole env of laravel, not sure if that is right or not - am currently running this locally.

Mentioned this here as well: spatie/browsershot#728

THANK YOUUUUUUU

acharmat commented 8 months ago

after much code diving, it seemed that the process of puppeteer being run by browsershot was not getting any environment variables, I traced the process creation to line 1032 in file vendor/spatie/browsershot/src/Browsershot.php $process = $this->isWindows() ? new Process($fullCommand) : Process::fromShellCommandline($fullCommand);

and modified

$process = $this->isWindows() ? new Process($fullCommand,null,getenv() ) : Process::fromShellCommandline($fullCommand);

now I am able to generate PDF Files. I know am not supposed to modify files in vendor folders - need to check further

also am sending the whole env of laravel, not sure if that is right or not - am currently running this locally.

Mentioned this here as well: spatie/browsershot#728

Thank you so much

you saved my day

KahiluChipango commented 7 months ago

Use this Instead

npm install puppeteer --location=global