spiritix/php-chrome-html2pdf

Getting an issue when running library on AWS EC2 linux server

bahadurvaibhav opened this issue · 7 comments

I am able to run the library perfectly on my laptop (Windows 10)

When I upload the same code to my server, I first got this error
ErrorException: fwrite(): write of 8192 bytes failed with errno=32 Broken pipe in file /var/www/html/pdf/vendor/spiritix/php-chrome-html2pdf/src/Spiritix/Html2Pdf/ProcessUtil.php on line 63

I made the pdf.html file very very simple, with the following code
image
But this is also giving me this error
Spiritix\Html2Pdf\ConverterException: Shell error: 127 in file /var/www/html/beta/api/vendor/spiritix/php-chrome-html2pdf/src/Spiritix/Html2Pdf/Converter.php on line 192

I am using a Laravel project
PHP version is 7.4.13

This is the code in my api.php
`<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MasterController;

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::get('/generatePdf', [MasterController::class,'generatePdf']);`

This is the code in my MasterController
`<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Spiritix\Html2Pdf\Converter;
use Spiritix\Html2Pdf\Input\StringInput;
use Spiritix\Html2Pdf\Output\FileOutput;

class MasterController extends Controller
{
public function generatePdf() {
// $input = new UrlInput();
// $input->setUrl('https://www.google.com');
$input = $this->getTemplate();
$stringInput = new StringInput();
$stringInput->setHtml($input);

    // echo $input;

    $converter = new Converter($stringInput, new FileOutput());

    $converter->setOption('potrait', true);

    $converter->setOptions([
        'printBackground' => true,
        'displayHeaderFooter' => true,
        'headerTemplate' => '<p>I am a header</p>',
    ]);

    $output = $converter->convert();
    // $output->download('google.pdf');
    $output->store(resource_path('views\pdf\store\google.pdf'));
}

public function getTemplate() {
    return file_get_contents(resource_path('views/pdf/pdf.html'));
}

}`

This is the pdf.html file (given in txt format as HTML is not supported when posting in GitHub issues
pdf.txt
)

I am able to get this library to work only if I start my laravel project using
"php artisan serve"

If I try to access the API, or PHP files directly (via URL) then it is giving the issues mentioned in the above post
fwrite() .....
shell error 127

I have got it to work on a Windows server. There is still some issue in running it from Linux server.

Shell error 127 usually means that the command (resp. the binary) could not be found. Try to set the path to the node executable manually using the $converter->setNodePath() method.

Thanks for the reply

Tried that as well.

I had set the path like so. Let me know if this is correct
$converter->setNodePath('/home/ec2-user/.nvm/versions/node/v17.2.0/bin/node')
I even tried this
$converter->setNodePath('/home/ec2-user/.nvm/versions/node/v17.2.0/bin')

After I set the node path, I started getting this error
Shell error 126

Shell error 126 means your Node binary is missing executable permissions.

Hi @bahadurvaibhav, i am facing the exact same issue as you. May i know if you have resolved it and how?

I was not able to solve the issue, but I got it to run on server

I had to use this command in server (the same one I was using in local machine)
php artisan serve --port=8080

Then I was able to connect to my API using http://SERVER_IP:8080/....

I was not able to solve the issue, but I got it to run on server

I had to use this command in server (the same one I was using in local machine) php artisan serve --port=8080

Then I was able to connect to my API using http://SERVER_IP:8080/....

Oh, this work around doesn't seem applicable for me. So weird that it works just fine on local but not on the server. I have also updated the permission of all the files but it still doesn't work.