Skip to content

Commit

Permalink
feat: improve error handling when running wkhtmltopdf
Browse files Browse the repository at this point in the history
  • Loading branch information
themightychris committed Nov 28, 2018
1 parent aa92bc4 commit d21402f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions php-classes/RequestHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,25 @@ public static function respondPrint($responseID, $responseData = array(), $forma
$tmpPath = tempnam('/tmp', 'e_pdf_');

file_put_contents($tmpPath.'.html', $html);

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.str_replace('"', '', $responseID).'.pdf"');

exec(implode(' ', [

$command = implode(' ', [
static::$wkhtmltopdfPath,
static::$wkhtmltopdfArguments,
escapeshellarg($tmpPath.'.html'),
escapeshellarg($tmpPath.'.pdf')
]));

]);

$output = exec($command);

if (!file_exists("$tmpPath.pdf")) {
header('HTTP/1.0 501 Not Implemented');
die('Unable to generate PDF, check that this system has wkhtmltopdf installed');
header('Content-Type: text/plain');
die("Unable to generate PDF, see command/output below and check that this system has wkhtmltopdf installed.\n\ncommand: {$command}\n\noutput: {$output}");
}


header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.str_replace('"', '', $responseID).'.pdf"');

readfile($tmpPath.'.pdf');
exit();
} elseif ($format == 'html') {
Expand Down

0 comments on commit d21402f

Please sign in to comment.