Skip to content

Instantly share code, notes, and snippets.

@pulponair
Created October 28, 2015 09:24
Show Gist options
  • Save pulponair/3b9f3a59840cb8993de3 to your computer and use it in GitHub Desktop.
Save pulponair/3b9f3a59840cb8993de3 to your computer and use it in GitHub Desktop.
public function generatePdfFromHtml($html) {
$descriptorSpecification = array(
0 => array('pipe', 'r'), // stdin
1 => array('pipe', 'w'), // stdout
2 => array('pipe', 'w'), // stderr
);
$process = proc_open($this->wkhtmltopdf . ' ' . implode(' ', $this->wkhtmltopdfParameters). ' - -',
$descriptorSpecification, $pipes);
fwrite($pipes[0], $html);
fclose($pipes[0]);
$pdf = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
fclose($pipes[1]);
$returnCode = proc_close($process);
if ($returnCode != 0 && $errors) {
throw new \Exception('PDF generation failed: ' . $errors);
}
return $pdf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment