'Laravel - wkhtmltopdf on OS X returning an error
I am using Laravel Snappy as a wrapper for WKHTMLToPDF, to allow my app to generate PDFs. Unfortunately, I am receiving this error:
The exit status code '126' says something went wrong:
stderr: "sh: /audit/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64:
cannot execute binary file
"
stdout: ""
command: /audit/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 --lowquality
I installed both 64 bit packages (PDF and Image) through Composer, following these instructions: https://github.com/KnpLabs/snappy#wkhtmltopdf-binary-as-composer-dependencies, and I can see the packages have installed within the /vendor/ folder of my app.
If I cd into the PDF directory and run sh ./wkhtmltopdf-amd64, it returns: cannot execute binary file.
Previously, I have run these two commands on the folder to try and stop this error: chmod a+x wkhtmltopdf-amd64 and sudo chmod +x wkhtmltopdf-amd64 - but neither appear to have resolved this issue of executing a binary file.
I am on the latest version of OS X, any help or guidance as to how I can get this working would be hugely appreciated.
Solution 1:[1]
I'm just now getting set up with the same configuration - MAMP, OSX, Laravel, Snappy.
I found that I had to download the OSX binary from here and install it: http://wkhtmltopdf.org/downloads.html
Once installed, the wkhtmltopdf binary will be in /usr/local/bin
Once installed, be sure you can run the command from Terminal.
Then, in your config/snappy.php file, be sure the 'binary' option is set to the proper path.
After this, mine works fine.
Solution 2:[2]
first of all go to http://wkhtmltopdf.org/downloads.html , then download the proper package according to your os . after installing the package do the followings :
in the config/snappy.php file , write the name of the {bin file} which has been installed in /usr/local/bin , which in my case is wkhtmltopdf :
'binary' => '/usr/local/bin/{bin file}'
so :
'binary' => '/usr/local/bin/wkhtmltopdf'
Solution 3:[3]
Here is what I did:
I had installed wkhtmltopdf on my Catalina Edition using the dmg installer file. So the "wkhtmltopdf" command works in the terminal. So all I had to do was instead of giving a path in the snappy.php, I simply passed the command name - "wkhtmltopdf".
So my snappy config looks like this:
'pdf' => [
'enabled' => true,
'binary' => 'wkhtmltopdf',
'timeout' => false,
'options' => [
'enable-local-file-access' => true,
'encoding' => 'UTF-8'
],
'env' => [],
],
And now this worked.
Solution 4:[4]
Are you using Homestead? I think some of the binary dependencies are Linux-based and won't work if installed on a Mac. I think!
Solution 5:[5]
Download and install the correct package according to your system here https://wkhtmltopdf.org/downloads.html
Make sure the binaries are loaded in '/usr/local/bin' directory.
Update your project .env by adding new entry as: WKHTML_PDF_BINARY='/usr/local/bin/wkhtmltopdf'
Make sure the correct binary path is obtained from the project .env file.
You should be able to generate PDF files now.
System:
- macOS Big Sur
- PHP 7.4
- Composer 2
- wkhtmltopdf 0.12.6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JR Lawhorne |
| Solution 2 | mre7_a |
| Solution 3 | Isru |
| Solution 4 | trh88 |
| Solution 5 | Ruwan Lokubalasooriya |
