Skip to content

Instantly share code, notes, and snippets.

@sashaaro
Last active July 14, 2023 19:09
Show Gist options
  • Save sashaaro/25244a6200585bc538d1bbe9f14f8593 to your computer and use it in GitHub Desktop.
Save sashaaro/25244a6200585bc538d1bbe9f14f8593 to your computer and use it in GitHub Desktop.
wkhtmltopdf generate pdf stream without temporary file
<?php
use Symfony\Component\Process\Process;
class DocumentGenerator
{
private function wkhtmltopdf(string $html): string
{
$wkhtmltopdfProcess = new Process('/usr/bin/wkhtmltopdf -q - -');
$wkhtmltopdfProcess->setInput($html);
$wkhtmltopdfProcess->start();
$exitCode = $wkhtmltopdfProcess->wait();
if (0 !== $exitCode) {
throw new DocumentGeneratorException(
'wkhtmltopdf process error: ' . $wkhtmltopdfProcess->getErrorOutput(),
$exitCode
);
}
if (substr($output, 0, 5) !== '%PDF-') {
throw new DocumentGeneratorException(
'wkhtmltopdf return not pdf output: ' . $wkhtmltopdfProcess->getOutput()
);
}
return $wkhtmltopdfProcess->getOutput();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment