Yii report using Jasper Reports

In this post I will show you how to integrate Jasper Reports into Yii framework so that you can run yii report. If you are not familiar with Jasper Reports you can know more about it by reading the post here. For running the report you will require to install the Jasper Reports Server. You can download the Jasper Reports server Community edition from http://community.jaspersoft.com/project/jasperreports-server.

After the installation is complete the Jasper Server will automatically run as windows service. You can also start the Jasper Server service from the start menu. You will find it under Start->All Programs->JasperReports Server CP->Start or Stop Services.You can get the login page from Start->All Programs->JasperReports Server CP->JasperReports Server Login. You can also get  the login page by typing the address http://localhost:8080/jasperserver/login.html on your browser’s address bar. Some time you may face the problem of Jasper Reports Server not starting. It may be because of a failed installation. In that case you can start the JasperReports Server by running the startup.bat file located at the bin directory of your JasperReports Server installation. In my case it is located at C:\Jaspersoft\jasperreports-server-cp-5.5.0\apache-tomcat\bin. If you were able to successfuly install the JasperReports Server you will see  the login page below.

yii report

You can login by entering the default user name and password which is ‘jasperadmin’. On successful login you will see the below dashboard.

yii report

In this example I will use Yii application as described in this post. For integrating JasperReports Server we will require the Yii extension Yiijasperserver which can be downloaded from here.This plugin requires PEAR XML_Serializer installed. Then extract the files and copy the folder into the ‘myapp\protected\extensions’ directory. Now we have to create a folder in the JasperReports Server for uploading the files. For that from the left side menu right click on the Reports link and select ‘Add Folder’. Enter the folder name as ‘myapp’ so as it is easily identifiable which  application it is using.

yii report

yii report

Next we will create the data source for our report. From the left menu right click on the ‘Data Sources’ menu and select “Add Resource’->’Data Source’. In the screen  that appears enter name as myapp_datasource, drivers as MySQL, host as localhost, port as 3306, database as report, User Name as root and leave password empty. Leave the other fields with default value. Then press the ‘Test Connection’ button. On successful create the data source.

yii report

yii report

Now we have to upload the report that we developed as described in this post to the JasperReports Server. For that first open the report file in the Jasper Studio. From the menu in the left side under ‘Repositor’ right click on the Servers and select create JasperReports Server Connection. In the dialog box that appears, enter the following details. Name:JasperReports Server, URL:http://localhost:8080/jasperserver/, User:jasperadmin, Password:jasperadmin. Then click on ‘Test Connection’ button.

yii report

yii report

Now click on the top right for publishing the report to Jasper Server. In the dialog that appears select myapp folder under Reports. Press next and in the next screen select the datasource as ‘myapp_datasource’ that we created earlier and click finish.

yii report

yii report

yii report

Next create a view file under ‘myapp\protected\views\site\marks_report.php’. In the file enter the below code and save.

$this->widget('ext.Yiijasperserver.Yiijasperserver',array(
'path'=>'/reports/myapp/marks',
'format'=>'pdf',
'out'=>'I',
'file'=>'Student Report',

)
);

Next create the action for displaying the view file in  ‘myapp\protected\controllers\SiteController.php’.

public function actionMarksReport()
{
$this->render('marks_report');
}

Now enter the below code in the menu section of the layout file located at ‘myapp\protected\views\layouts\main.php’.

array('label'=>'Marks Report', 'url'=>array('/site/marksreport')),

Now click on the ‘Marks Report’ menu, you will see the report in PDF format.

yii report