Jump to content
Changes to the Jaspersoft community edition download ×

How to Connect Jasper reports in PHP application


thaniindia

Recommended Posts

I have installed ubuntu server with php mysql webapplication. Also installed jasperserver 5.5.0 in the same server.

Now I have designed reports using iReports and placed in server /var/www/ directory. Now I want to display this report in pdf/xls in client's browser.

I have searched many guides, but doesnot work out for me. Can help me the step by step how to ?

Thank you, Thanika

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

First, you must add your reports to your instance of JasperServer.  Once you've done this, each report will have a specific URI relative to its location on the report server.

You should then take a look at the PHP Client for JasperReports, which you can find on GitHub: https://github.com/Jaspersoft/jrs-rest-php-client

Once you've installed the PHP client, you will be able to make requests within your PHP application to obtain and display reports in whichever format you prefer (iframe, offer for download, etc)

An example to help you get started:

If you have a report located at the URI "/reports/MyReport" you will be able to display it to users in an HTML format using the following PHP code:

<?phprequire_once __DIR__ . "/vendor/autoload.php";header('Content-Type: text/html');use JaspersoftClientClient;$c = new Client(        "localhost",        "8080",        "jasperadmin",        "jasperadmin",        "/jasperserver-pro",        "organization_1"      );  $report = $c->reportService()->runReport('/reports/MyReport', 'html');echo $report;  ?>[/code]

 

In this simple example, a client connection is created and stored in $c using the credentials supplied in the instansiation. You can then use any of the services provided by Client to access various functionality of the JasperReports Server.

For displaying reports, you will want to use the report service. In the example above, we ask the client to provide the report located at the URI "/reports/MyReport" in HTML format. It is then stored in $report.

Since the report is in HTML, you can then display it simply by echoing the content of $report.

Link to comment
Share on other sites

my existing server is running jasper reports using php-java bridge. my ex-colleague installed that server. I have no idea how he was installed. but currently we are using jasper ireports using the following php coding. we do not go to jasperserver to upload the reports. we put all our reports under 'reports' folder under '/var/www/reports' and specified the path in php file. can do the same thing?

---------------------------------------------------------------------

 

<?php session_start(); ?>

<?php ob_start(); ?>

<?php

require_once("http://localhost:8080/Java.inc");

 

/**

* see if the java extension was loaded.

*/

function checkJavaExtension()

{

if(!extension_loaded('java'))

{

$sapi_type = php_sapi_name();

$port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'])>1024)) ? $_SERVER['SERVER_PORT'] : '8080';

if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli")

{

if(!(PHP_SHLIB_SUFFIX=="so" && @dl('java.so'))&&!(PHP_SHLIB_SUFFIX=="dll" && @dl('php_java.dll'))&&!(@include_once("java/Java.inc"))&&!(require_once("http://127.0.0.1:$port/java/Java.inc")))

{

error_log("extension not installed");

return "java extension not installed.";

}

}

else

{

if(!(@include_once("java/Java.inc")))

{

error_log("Requiring");

require_once("http://127.0.0.1:$port/java/Java.inc");

}

}

}else{

error_log("No exension loaded");

error_log(serialize(get_loaded_extensions()));

}

if(!function_exists("java_get_server_name"))

{

return "The loaded java extension is not the PHP/Java Bridge";

}

 

return true;

}

 

/**

* convert a php value to a java one...

* @param string $value

* @param string $className

* @returns boolean success

*/

function convertValue($value, $className)

{

// if we are a string, just use the normal conversion

// methods from the java extension...

try

{

if ($className == 'java.lang.String')

{

$temp = new Java('java.lang.String', $value);

return $temp;

}

else if ($className == 'java.lang.Boolean' ||

$className == 'java.lang.Integer' ||

$className == 'java.lang.Long' ||

$className == 'java.lang.Short' ||

$className == 'java.lang.Double' ||

$className == 'java.math.BigDecimal')

{

$temp = new Java($className, $value);

return $temp;

}

else if ($className == 'java.sql.Timestamp' ||

$className == 'java.sql.Time')

{

$temp = new Java($className);

$javaObject = $temp->valueOf($value);

return $javaObject;

}

}

catch (Exception $err)

{

echo ( 'unable to convert value, ' . $value .

' could not be converted to ' . $className);

return false;

}

 

echo ( 'unable to convert value, class name '.$className.

' not recognised');

return false;

}

 

 

//Get Query string values

$username = $_SESSION['loginid'];

$report_format=$_GET['report_format'];

$date = date('Y-m-d');

$report_date_time = date("F j, Y, g:i a");

$report_date=date('d-m-Y');

 

$sortby1=$_GET['sortby1'];

$sortby2=$_GET['sortby2'];

$sortby3=$_GET['sortby3'];

 

$report_header="STUDENT REQUEST/ISSUES PENDING/OUTSTANDING REPORT AS AT {$report_date} ";

$order_by=" ORDER BY {$sortby1},{$sortby2},{$sortby3}";

 

checkJavaExtension();

$class = new JavaClass('java.lang.Class');

$class->forName('com.mysql.jdbc.Driver');

$driverManager = new JavaClass('java.sql.DriverManager');

 

$conn = $driverManager->getConnection("jdbc:mysql://".DB_SERVER."/".DB_NAME."?user=".DB_USER."&password=".DB_PASS);

 

if($report_format=='pdf')

{

$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");

 

//report path and name

$report = $compileManager->compileReport(realpath("reports/pending_request.jrxml"));

 

$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");

 

//Assign Parameters

$param = new Java("java.util.HashMap");

$param->put("report_filter", $report_filter);

$param->put("order_by", $order_by);

$param->put("report_date", $report_date);

$param->put("report_date_time", $report_date_time);

$param->put("report_header", $report_header);

 

$jasperPrint = $fillManager->fillReport($report, $param, $conn);

 

$outputPath = realpath(".")."/"."outputs/Pending_Request.pdf";

 

$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");

$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);

 

header("Content-type: application/pdf");

readfile($outputPath);

 

unlink($outputPath);

}

?>

 

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...