1<?php
2
3function safe_exec($cmd, &$output) {
4  exec($cmd, $output, $result);
5
6  if ($result) {
7    $message = "";
8
9    if (count($output) > 0) {
10      $message .= "Error executing '{$cmd}'<br/>\n";
11      error_log("Error executing '{$cmd}'.");
12      $message .= "Command produced the following output:<br/>\n";
13      error_log("Command produced the following output:");
14
15      foreach ($output as $line) {
16        $message .= "{$line}<br/>\n";
17        error_log($line);
18      };
19    } else {
20      $_cmd = $cmd;
21      include(HTML2PS_DIR.'templates/error_exec.tpl');
22      error_log("Error executing '{$cmd}'. Command produced no output.");
23      die("HTML2PS Error");
24    };
25    die($message);
26  };
27}
28
29class OutputFilterPS2PDF extends OutputFilter {
30  var $pdf_version;
31
32  function content_type() {
33    return ContentType::pdf();
34  }
35
36  function _mk_cmd($filename) {
37    return GS_PATH." -dNOPAUSE -dBATCH -dEmbedAllFonts=true -dCompatibilityLevel=".$this->pdf_version." -sDEVICE=pdfwrite -sOutputFile=".$filename.".pdf ".$filename;
38  }
39
40  function OutputFilterPS2PDF($pdf_version) {
41    $this->pdf_version = $pdf_version;
42  }
43
44  function process($tmp_filename) {
45    $pdf_file = $tmp_filename.'.pdf';
46    safe_exec($this->_mk_cmd($tmp_filename), $output);
47    unlink($tmp_filename);
48    return $pdf_file;
49  }
50}
51?>