Monday, December 6, 2010

How to connect php with crystal report

There are many people who are developing the web-based interface. It is alway necessary to design user reports to show the ouputs. Crystal report is mostly trusted report desinging too and very famous among the developers. I was searching to connect crystal reports with PHP and finally I got some good hints about connection both. I designed a seperate function by calling which my task was so easy. Here is the code for the function. It uses COM component to connect with PHP.

[php]
function ViewCRReport($exp_pdf,$my_report,$userid,$password,$parameter)
{
$ObjectFactory= New COM("CrystalReports10.ObjectFactory.1");
$crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application");
$creport = $crapp->OpenReport($my_report, 1);
$creport->Database->Tables->Item(1)->ConnectionProperties['User ID'] = "$userid";
$creport->Database->Tables->Item(1)->ConnectionProperties['Password'] = "$password";
$creport->FormulaSyntax = 0;

//This is it, this avoids the hanging! $creport->EnableParameterPrompting = 0;

$creport->DiscardSavedData;
//it is essential to assign the param values here after the readrecords otherwise you will not pass anything...

foreach($parameter as $i => $value)
{
$zz1= $creport->ParameterFields($i+1)->SetCurrentValue("$value"); }
$creport->ReadRecords();
$creport->ExportOptions->DiskFileName=$exp_pdf;
// $creport->ExportOptions->PDFExportAllPages=true;
$creport->ExportOptions->DestinationType=1;
$creport->ExportOptions->FormatType=31;
$creport->Export(false); $
creport = null;
$crapp = null;
$ObjectFactory = null;
return 1;

}
[php]

2 comments:

Amal George said...

i like it i wanna knw mre abt php plse help me to understand...

S H A K I L A H A M A D said...

I don't know what to pass for "$parameter" for this. Can anyone help me plz.....