Created
December 13, 2018 12:37
-
-
Save michaelthieulin/839b2f96778f33f8743bb5c386d9993a to your computer and use it in GitHub Desktop.
Chrome headless with Symfony/Process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Lib\Core\Export; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\Process\Process; | |
class PdfExport | |
{ | |
public function run($infile, $outfile, LoggerInterface $logger): bool | |
{ | |
$process = new Process( | |
[ | |
'/usr/bin/chromium-browser', | |
'--headless', | |
'--disable-gpu', | |
'--disable-software-rasterizer', | |
'--disable-dev-shm-usage', | |
'--run-all-compositor-stages-before-draw', | |
'--no-margins', | |
'--no-sandbox', | |
'--print-to-pdf=' . $outfile, | |
$infile, | |
], null, null, null, null); | |
$logger->debug('Init process to convert HTML to PDF', ['cmdline' => $process->getCommandLine()]); | |
$process->start(); | |
$logger->debug('Process started', ['pid' => $process->getPid()]); | |
$process->wait(function ($type, $buffer) use ($logger) { | |
if (Process::ERR === $type) { | |
$logger->debug('Process stderr', ['msg' => $buffer]); | |
} else { | |
$logger->debug('Process stdout', ['msg' => $buffer]); | |
} | |
}); | |
$logger->debug('Process exit code', ['code' => $process->getExitCode()]); | |
$process->stop(0); | |
$logger->debug('Process stopped'); | |
return $process->isSuccessful(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment