59 lines
1.6 KiB
PHP
59 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Models\AppSetting;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\View;
|
||
|
|
|
||
|
|
class ExportHelpPdf extends Command
|
||
|
|
{
|
||
|
|
protected $signature = 'help:export-pdf {--output= : Output path for the PDF file}';
|
||
|
|
protected $description = 'Genera il PDF della guida completa';
|
||
|
|
|
||
|
|
public function handle(): int
|
||
|
|
{
|
||
|
|
$appName = AppSetting::getAppName() ?? 'Glastree';
|
||
|
|
|
||
|
|
$this->components->info('Generazione HTML della guida...');
|
||
|
|
|
||
|
|
$html = View::make('help.pdf', ['appName' => $appName])->render();
|
||
|
|
|
||
|
|
$tmpHtml = tempnam(sys_get_temp_dir(), 'help_') . '.html';
|
||
|
|
file_put_contents($tmpHtml, $html);
|
||
|
|
|
||
|
|
$output = $this->option('output');
|
||
|
|
if (!$output) {
|
||
|
|
$output = storage_path('app/help-guida.pdf');
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->components->info('Conversione in PDF via Chromium...');
|
||
|
|
|
||
|
|
$cmd = sprintf(
|
||
|
|
'/usr/bin/chromium --headless --no-sandbox --disable-gpu --print-to-pdf=%s %s 2>&1',
|
||
|
|
escapeshellarg($output),
|
||
|
|
escapeshellarg('file://' . $tmpHtml)
|
||
|
|
);
|
||
|
|
|
||
|
|
exec($cmd, $execOutput, $exitCode);
|
||
|
|
|
||
|
|
unlink($tmpHtml);
|
||
|
|
|
||
|
|
if ($exitCode !== 0) {
|
||
|
|
$this->components->error('Errore Chromium: ' . implode("\n", $execOutput));
|
||
|
|
return Command::FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
$size = filesize($output);
|
||
|
|
$this->components->success(sprintf(
|
||
|
|
'PDF generato: %s (%.2f MB)',
|
||
|
|
$output,
|
||
|
|
$size / 1024 / 1024
|
||
|
|
));
|
||
|
|
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|