getAuthUser(); if (!$au) { $this->jerr("access denied"); } } function get() { // accept // param : // template // filemanem $args = array( 'template' => $_REQUEST['template'] ); if (isset($_REQUEST['param'])) { $args['param'] = $_REQUEST['param']; } $this->toPdf($args, $_REQUEST['filename']); } /** * run rptrender and output the file.. * * @param array $cfg configuration - param=> ... => loadfromdb=> * @param filename * */ function toPdf($cfg, $name) { if (isset($cfg['template'])) { $cfg['template'] = rtrim($cfg['template'],'-'); } $ocfg = $cfg; $do = DB_DataObject::Factory('invchead'); $dsn = $do->getDatabaseConnection()->dsn; $office = substr($do->database(),-2); $fn = tempnam(sys_get_temp_dir(),'print').'.pdf'; $args = array( 'databaseURL'=> 'psql://' . $dsn["hostspec"] .'/'. $dsn["database"] .':5432', 'username' => $dsn["username"], 'passwd' => $dsn["password"], 'pdf' => true, 'outpdf' => $fn, //'param' => "invchead_id:integer='{$this->invchead_id}'", //'loadfromdb' => 'Invoice', 'close' => true ); /* $template = ''; if (isset($cfg['template'])) { $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml'; unset($cfg['template']); } // do we have a localized version??? $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template); //$this->jerr($template_test); if (file_exists($template_test)) { $template = $template_test; } if (!file_exists($template)) { // see if exists in the database.. $r = DB_DAtaObject::factory('report'); if (!$r->get('report_name', $ocfg['template'])) { $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain'); } $args['loadfromdb'] = escapeshellarg($ocfg['template']); $template = ''; } */ $args['loadfromdb'] = escapeshellarg($this->syncDB($cfg)); unset($cfg['template']); // -loadfromdb={report_name} $args = array_merge($cfg,$args); $cmd = array(); require_once 'System.php'; $xvfb = System::which('xvfb-run'); if (!$xvfb ) { $this->jerr("no xvfb-run found", array(), 'text/plain'); } $rpt = System::which('rptrender'); if (!$rpt) { $this->jerr("no report renderer", array(), 'text/plain'); } $cmd[] = "$xvfb --auto-servernum"; $cmd[] = $rpt; foreach($args as $k=>$v) { if (is_array($v)) { foreach($v as $vv) { $cmd[] = '-'.$k . ($vv === true ? '' : '='. $vv); } continue; } $cmd[] = '-'.$k . ($v === true ? '' : '='. $v); } $cmd = implode(' ', $cmd) ; //$cmd = implode(' ', $cmd) . ' ' . (strlen($ocfg['template']) ? escapeshellarg($template) : ''); if (isseT($_REQUEST['_debug'])) { echo $cmd; exit; } // echo $cmd;exit; $res = `$cmd`; // var_dump( $res); if (!file_exists($fn)) { $this->jerr("File creation failed: " . $cmd, array(), 'text/plain'); } require_once 'File/Convert.php'; $x = new File_Convert($fn, "application/pdf"); $x->convert("application/pdf"); $x->serve('attachment', $name .'.pdf', true); // delte after... exit; } function syncDB($cfg) { $dir = HTML_FlexyFramework::get()->Pman; $do = DB_DataObject::Factory('invchead'); $office = substr($do->database(),-2); $ocfg = $cfg; $template = ''; if (isset($cfg['template'])) { $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml'; if(isset($dir['report_template_dir'])){ $template = realpath($dir['report_template_dir']). '/'.$cfg['template']. '.xml'; } unset($cfg['template']); } // do we have a localized version??? $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template); //$this->jerr($template_test); if (file_exists($template_test)) { $template = $template_test; } // template now == full path with .xml at the end.. if (!file_exists($template)) { // see if exists in the database.. $r = DB_DAtaObject::factory('report'); if (!$r->get('report_name', $ocfg['template'])) { $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain'); } return $ocfg['template']; } // we have a file, and we need to sync it with database. $mt = filemtime($template); $r = DB_DAtaObject::factory('report'); if (!$r->get('report_name', $ocfg['template'])) { // insert one.. $r = DB_DAtaObject::factory('report'); $r->setFrom(array( 'report_name' => $ocfg['template'], 'report_source' => file_get_contents($template), 'report_grade' => 0, 'report_loaddate' => date('Y-m-d H:i:s', $mt) )); $r->insert(); return $ocfg['template']; } // see if it needs updating.. if (strtotime($r->report_loaddate) < $mt) { $r->setFrom(array( 'report_source' => file_get_contents($template), 'report_loaddate' => date('Y-m-d H:i:s', $mt) )); $r->update(); return $ocfg['template']; } // system is newer than ours.. return $ocfg['template']; } }