Skip to content

Instantly share code, notes, and snippets.

@michaelthieulin
Created December 13, 2018 12:37
Show Gist options
  • Select an option

  • Save michaelthieulin/839b2f96778f33f8743bb5c386d9993a to your computer and use it in GitHub Desktop.

Select an option

Save michaelthieulin/839b2f96778f33f8743bb5c386d9993a to your computer and use it in GitHub Desktop.

Revisions

  1. michaelthieulin created this gist Dec 13, 2018.
    49 changes: 49 additions & 0 deletions PdfExport.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?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();
    }
    }