/**
  * Renders passed in content to a PDF.
  *
  * If $outputTo == '', then the temporary filename is returned, with the expectation
  * that the caller will correctly handle the streaming of the content.
  *
  * @param string $content
  * 			Raw content to render into a pdf
  * @param string $outputTo
  * 				'file' or 'browser'
  * @param string $outname
  * 				A filename if the pdf is sent direct to the browser
  * @return string
  * 				The filename of the output file
  */
 public function render($content, $outputTo = null, $outname = '')
 {
     $tempFolder = getTempFolder();
     if (!is_dir($tempFolder)) {
         throw new Exception("Could not find TMP directory");
     }
     $pdfFolder = $tempFolder . '/pdfrenditions';
     if (!file_exists($pdfFolder)) {
         @mkdir($pdfFolder);
     }
     if (!is_dir($pdfFolder)) {
         throw new Exception("PDF temp directory could not be found");
     }
     $in = tempnam($pdfFolder, "html_");
     chmod($in, 0664);
     $content = $this->fixLinks($content);
     $content = str_replace(' ', ' ', $content);
     $content = http::absoluteURLs($content);
     file_put_contents($in, $content);
     $mid = tempnam($pdfFolder, "xhtml_");
     chmod($mid, 0664);
     $out = tempnam($pdfFolder, "pdf_") . '.pdf';
     if (class_exists('Tidy')) {
         $this->tidyHtml($in, $mid);
     } else {
         $this->tidyHtmlExternal($in, $mid);
     }
     // then run it through our pdfing thing
     $jarPath = dirname(dirname(dirname(__FILE__))) . '/thirdparty/xhtmlrenderer';
     $classpath = $jarPath . '/flying-saucer-core-9.0.7.jar' . PATH_SEPARATOR . $jarPath . '/flying-saucer-pdf-9.0.7.jar' . PATH_SEPARATOR . $jarPath . '/itext-4.2.1.jar';
     $cmd = self::$java_bin;
     if (!is_executable($cmd)) {
         $cmd = "java";
     }
     $escapefn = 'escapeshellarg';
     $cmd = "{$cmd} -classpath " . $escapefn($classpath) . " org.xhtmlrenderer.simple.PDFRenderer " . $escapefn($mid) . ' ' . $escapefn($out);
     $retVal = exec($cmd, $output, $return);
     if (!file_exists($out)) {
         throw new Exception("Could not generate pdf using command {$cmd}: " . var_export($output, true));
     }
     unlink($in);
     unlink($mid);
     if (!($outputTo == 'browser')) {
         return $out;
     }
     if (file_exists($out)) {
         $size = filesize($out);
         $type = "application/pdf";
         $name = urlencode(htmlentities($outname));
         if (!headers_sent()) {
             // set cache-control headers explicitly for https traffic, otherwise no-cache will be used,
             // which will break file attachments in IE
             // Thanks to Niklas Forsdahl <*****@*****.**>
             if (isset($_SERVER['HTTPS'])) {
                 header('Cache-Control: private');
                 header('Pragma: ');
             }
             header('Content-disposition: attachment; filename=' . $name);
             header('Content-type: application/pdf');
             //octet-stream');
             header('Content-Length: ' . $size);
             readfile($out);
         } else {
             echo "Invalid file";
         }
     }
     unlink($out);
 }