Skip to content

Instantly share code, notes, and snippets.

@michaelthieulin
Created December 13, 2018 12:37
Show Gist options
  • Save michaelthieulin/839b2f96778f33f8743bb5c386d9993a to your computer and use it in GitHub Desktop.
Save michaelthieulin/839b2f96778f33f8743bb5c386d9993a to your computer and use it in GitHub Desktop.
Chrome headless with Symfony/Process
<?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