Created
April 10, 2012 11:15
-
-
Save np422/2350554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Konstanter tills vidare, senare från params .... | |
define('REPORT_URI' , '/reports/samples/AllAccounts'); | |
define('JASPER_SERVER_URL' , 'http://localhost:8080/jasperserver/'); | |
define('JASPER_USER' , 'test'); | |
define('JASPER_PASSWORD' , 'test'); | |
$xmlfile_name = tempnam ("/tmp", "XML"); | |
store_resource_descriptor($xmlfile_name); | |
#TODO: Bearbeta $xmlfile_name och stoppa in parametrar i filen om | |
$curl = curl_factory(); | |
$report_descriptor = run_report($curl,$xmlfile_name); | |
$pattern = "/\<uuid\>(.*)\<\/uuid\>.*\<file type=\"application\/pdf\"\>\<\!\[CDATA\[(.*)\]\]\>\<\/file\>/s"; | |
preg_match_all($pattern, $report_descriptor , $matches); | |
$uuid = $matches[1][0]; | |
$filename = $matches[2][0]; | |
$report_data = get_report($curl,$uuid,$filename); | |
send_to_client($report_data); | |
curl_close($curl); | |
//slut-i-rutan | |
function curl_factory() { | |
$curl = curl_init(); | |
$ckfile = tempnam ("/tmp", "CURLCOOKIE"); | |
// Login , skaffa kakor! | |
$login_url = JASPER_SERVER_URL . "rest/login?j_username=" . JASPER_USER . "&j_password=" . JASPER_PASSWORD; | |
curl_setopt($curl, CURLOPT_URL, $login_url ); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($curl, CURLOPT_HEADER, FALSE); | |
curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE); | |
curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile); | |
$data = curl_exec($curl); | |
return $curl; | |
} | |
function store_resource_descriptor($filename) { | |
$curl = curl_factory(); | |
$resource_url = JASPER_SERVER_URL . "rest/resource" . REPORT_URI; | |
curl_setopt($curl, CURLOPT_URL, $resource_url ); | |
$data = curl_exec($curl); | |
$xmlfile = fopen($filename , "w"); | |
fwrite($xmlfile,$data); | |
fclose($xmlfile); | |
} | |
#TODO: rapportformat som parameter | |
function run_report($curl,$filename) { | |
$run_url = JASPER_SERVER_URL . "rest/report"; | |
$xmlfile_size = filesize($filename); | |
$xmlfile = fopen($filename,'r'); | |
curl_setopt($curl, CURLOPT_PUT , TRUE); | |
curl_setopt($curl, CURLOPT_INFILE, $xmlfile ); | |
curl_setopt($curl, CURLOPT_INFILESIZE , $xmlfile_size); | |
curl_setopt($curl, CURLOPT_URL, $run_url); | |
$data = curl_exec($curl); | |
return $data; | |
} | |
function get_report($curl,$uuid,$filename) { | |
$report_url = JASPER_SERVER_URL . "rest/report/" . $uuid . "?file=" . $filename; | |
curl_setopt($curl, CURLOPT_URL, $report_url); | |
curl_setopt($curl, CURLOPT_PUT , FALSE); | |
$report_data = curl_exec($curl); | |
return $report_data; | |
} | |
function send_to_client($data) { | |
header('Content-type: application/pdf'); | |
header('Content-Disposition: attachment; filename="report.pdf"'); | |
echo $data; | |
flush(); | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment