Last active
July 14, 2023 19:09
-
-
Save sashaaro/25244a6200585bc538d1bbe9f14f8593 to your computer and use it in GitHub Desktop.
wkhtmltopdf generate pdf stream without temporary file
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 | |
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