note 85253 added to class.com
| From: | johnno1985 at gmail dot com | Date: | Fri, 22 Aug 2008 06:34:18 +0000 |
| Subject: | note 85253 added to class.com | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143735@lists.php.net to get a copy of this message | ||
I couldn't find an example anywhere on what I was trying to do. After taking bits from
everyone I ended up with something that worked.
Im using Crystal Reports 10, MS SQL Server 2005, Apache2, and PHP5.
My report uses an SQL Store Procedure and passes some parameter to get the report data, so our PHP
must set the database logon info and pass the parameters.
Below is the solution I came up with:
<?php
//------ Variables ------
$my_report = "C:\\Apache2\htdocs\\test\\MyReport.rpt"; //This must be the full path to
the file
$my_pdf = "C:\\Apache2\htdocs\\test\\MyReport.pdf";
//------ Create a new COM Object of Crytal Reports 10 ------
$ObjectFactory= new COM("CrystalReports10.ObjectFactory.1");
//------ Create a instance of library Application -------
$crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application.10");
//------ Open your rpt file ------
$creport = $crapp->OpenReport($my_report, 1);
//------ Set database logon info ------
$creport->Database->Tables(1)->SetLogOnInfo("MYSERVER", "Database",
"user", "password");
//------ Suppress the Parameter field prompt or else report will hang ------
$creport->EnableParameterPrompting = 0;
//------ DiscardSavedData make a Refresh in your data -------
$creport->DiscardSavedData;
$creport->ReadRecords();
//------ Pass formula fields --------
$creport->FormulaFields->Item(1)->Text = ("'My Report Title'");
$creport->ParameterFields(1)->AddCurrentValue ("FirstParameter");
$creport->ParameterFields(2)->AddCurrentValue (2000);
//------ Export to PDF -------
$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->FormatType=31;
$creport->ExportOptions->DestinationType=1;
$creport->Export(false);
//------ Release the variables ------
$creport = null;
$crapp = null;
$ObjectFactory = null;
//------ Embed the report in the webpage ------
print "<embed src=\"MyReport.pdf\" width=\"100%\"
height=\"100%\">"
?>
Obviously there's plenty of room for refinement in there, but it works and should get you going
on the right track. I also found that during testing, the script was hanging before supressing the
parameter prompt. Once this had happened, some exports that had been working earlier stopped
completely and wouldn't work again untill I restarted Apache. This might be something worth
doing if you keep having issues after a script has hung.
----
Server IP: 203.88.112.190
Probable Submitter: 203.17.42.5
----
Manual Page -- http://www.php.net/manual/en/class.com.php
Edit -- https://master.php.net/note/edit/85253
Del: integrated -- https://master.php.net/note/delete/85253/integrated
Del: useless -- https://master.php.net/note/delete/85253/useless
Del: bad code -- https://master.php.net/note/delete/85253/bad+code
Del: spam -- https://master.php.net/note/delete/85253/spam
Del: non-english -- https://master.php.net/note/delete/85253/non-english
Del: in docs -- https://master.php.net/note/delete/85253/in+docs
Del: other reasons-- https://master.php.net/note/delete/85253
Reject -- https://master.php.net/note/reject/85253
Search -- https://master.php.net/manage/user-notes.php