hasIdentity() && ($userId = App_User_Auth::getInstance()->getData('id'))) { $params[] = '--custom-header-propagation'; $params[] = '--custom-header x-pdf-user-id ' . intval($userId); } $pattern = self::_getRenderCmdPattern(); $placeholders = array( '{pageHeader}' => escapeshellarg($options['pageHeader']), '{pageFooter}' => escapeshellarg($options['pageFooter']), '{title}' => escapeshellarg($options['title']), '{url}' => escapeshellarg(Qs_Auth::addAuthCredentials($resourceUrl)), '{file}' => escapeshellarg($file), '{params}' => implode(' ', $params), ); $cmd = str_replace(array_keys($placeholders), array_values($placeholders), $pattern); exec($cmd, $out, $code); if (0 == $code && file_exists($file)) { chmod($file, 0644); } return array( 'file' => $file, 'code' => $code, 'out' => $out, ); } protected static function _getRenderCmdPattern() { static $command; if (null === $command) { $cfg = Zend_Registry::get('config')->app; $command = $cfg->part->pdfCommand; } if (null == $command) { throw new Exception('PDF rendering command can not be empty'); } return $command; } public static function sendTempFile($file, $name) { header('Expires: Sat, 01 Jan 2000 00:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="' . self::escapeAttachmentName($name . '.pdf') . '"'); readfile($file); unlink($file); exit; } public static function escapeAttachmentName($name) { $disabledCharacters = ['<', '>', '\\', '"', '/', ':', '|', '?', '*']; return str_replace($disabledCharacters, '', $name); } public static function getUniqueFileName($filename) { $pathInfo = pathinfo($filename); $index = 0; $uniqueName = $filename; while (file_exists($uniqueName) && $index <= 20) { ++$index; $uniqueName = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . ' (' . $index . ').' . $pathInfo['extension']; } return $index > 20 ? false : $uniqueName; } }