You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. //https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
  3. class PdfPrinter {
  4. private $binPath;
  5. private $config;
  6. function __construct() {
  7. global $config;
  8. $this->config = $config;
  9. $this->binPath = $this->config['settings']['wkhtmltopdf-bin-path'];
  10. }
  11. //https://gist.github.com/davejamesmiller/1965886
  12. function convertHtmlToPdf($html='', $headerURL=null, $footerURL=null) {
  13. $descriptorspec = array(
  14. 0 => array('pipe', 'r'), // stdin
  15. 1 => array('pipe', 'w'), // stdout
  16. 2 => array('pipe', 'w'), // stderr
  17. );
  18. $httpUser = $this->config['settings']['http-username'];
  19. $httpPass = $this->config['settings']['http-password'];
  20. $process = proc_open($this->binPath.' -q -B 2cm -T 2cm --username '.$httpUser.' --password '.$httpPass.' --header-html "'.$headerURL.'" --footer-html "'.$footerURL.'" - -', $descriptorspec, $pipes);
  21. // Send the HTML on stdin
  22. fwrite($pipes[0], $html);
  23. fclose($pipes[0]);
  24. // Read the outputs
  25. $pdf = stream_get_contents($pipes[1]);
  26. $errors = stream_get_contents($pipes[2]);
  27. // Close the process
  28. fclose($pipes[1]);
  29. $return_value = proc_close($process);
  30. // Output the results
  31. if ($errors) {
  32. file_put_contents(PUBLIC_HTML.'pdf_errors.log', $errors);
  33. //return false;
  34. }
  35. return $pdf;
  36. }
  37. }