How to Connect Jasper reports in PHP application

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

thaniindia's picture
Joined: Dec 25 2013 - 1:46am
Last seen: 5 years 1 day ago

1 Answer:

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:

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:

require_once __DIR__ . "/vendor/autoload.php";
header('Content-Type: text/html');
use Jaspersoft\Client\Client;
$c = new Client(
$report = $c->reportService()->runReport('/reports/MyReport', 'html');
echo $report;  

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.

grantbacon's picture
Joined: Oct 23 2012 - 1:10pm
Last seen: 5 years 3 months ago

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(); ?>

* see if the java extension was loaded.
function checkJavaExtension()
$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(''))&&!(PHP_SHLIB_SUFFIX=="dll" && @dl('php_java.dll'))&&!(@include_once("java/"))&&!(require_once("$port/java/")))
error_log("extension not installed");
return "java extension not installed.";
error_log("No exension loaded");
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...
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'];
$date = date('Y-m-d');
$report_date_time = date("F j, Y, g:i a");


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

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

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

$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");


thaniindia - 9 years 8 months ago

hello can you teach me how to use php javabridge? because I'm having a problem with my report pdf in php .. it does not display the table tool in php but it displayed in ireport when previewed .. Thank you .. :)

schneizerbp - 4 years 10 months ago