Pass Date Parameter From PHP to Crystal Report - php

I am having substantial trouble passing a date parameter to the Crystal Reports 11 component from PHP5 on Windows. It should be easy, of course, but the various commented-out items don't seem to work:
<?php
$my_report = "C:\\xampp\htdocs\wincare\laporan\adm_JumlahPasienPoli.rpt"; // rpt source file
$my_pdf = "C:\\xampp\htdocs\wincare\laporan\adm_JumlahPasienPoli.pdf"; // RPT export to pdf file
//-Create new COM object-depends on your Crystal Report version
$ObjectFactory= new COM("CrystalReports115.ObjectFactory.1") or die ("Error on load"); // call COM port
$crapp = $ObjectFactory-> CreateObject("CrystalDesignRunTime.Application.11"); // create an instance for Crystal
$creport = $crapp->OpenReport($my_report,1); // call rpt report
// to refresh data before
//- Set database logon info - must have
$creport->Database->Tables(1)->SetLogOnInfo("localhost", "db_wincare", "sa", "sa");
//- field prompt or else report will hang - to get through
$creport->EnableParameterPrompting = 0;
// this is the error
$zz = $creport->ParameterFields(1)->SetCurrentValue("2011-01-01 00:00:00");
//export to PDF process
$creport->ExportOptions->DiskFileName=$my_pdf; //export to pdf
$creport->ExportOptions->PDFExportAllPages=true;
$creport->ExportOptions->DestinationType=1; // export to file
$creport->ExportOptions->FormatType=31; // PDF type
$creport->Export(false);
//------ Release the variables ------
$creport = null;
$crapp = null;
$ObjectFactory = null;
//------ Embed the report in the webpage ------
print "<embed src=\"adm_JumlahPasienPoli.pdf\" width=\"100%\" height=\"100%\">"
?>
and the messege :
Fatal error: Uncaught exception 'com_exception' with message
'Source: Description: ' in
C:\xampp\htdocs\wincare\laporan\pakai.php:36 Stack trace: #0
C:\xampp\htdocs\wincare\laporan\pakai.php(36):
variant->SetCurrentValue('2011-01-01 00:0...') #1 {main} thrown in
C:\xampp\htdocs\wincare\laporan\pakai.php on line 36

I remember spending a long time on this question some five years ago, and eventually finding a hacky but working answer:
// This block is strictly guesswork
$application = new COM("CrystalRuntime.Application.9"); // Change to your version
$report = $application->OpenReport($my_report,1); // From OP's code
$rptParams = $report.ParameterFields
$rptParam = $rptParams->Item(2); // From my SitePoint post;
// obviously you need to use
// the right index
// Check that $rptParam->ValueType evaluates to 10 - if it does not
// then modify the type in Crystal Reports itself. Again, see my
// original solution
// This bit should be fine
$oScript = new COM("MSScriptControl.ScriptControl");
$oScript->Language = "VBScript";
$oScript->AllowUI = false;
$oScript->AddObject('rptParam', $rptParam, true);
$oScript->AddCode('Function SetDateParameter(strDate)
rptParam.AddCurrentValue(CDate(strDate))
End Function');
$oScript->Run("SetDateParameter", "25 April 2006");
This worked fine, but it's not very elegant! Worked on CR9 with Windows Server 2003, I think. Copied from here - was prior to the birth of StackExchange :).

Related

phpspreadsheet write 10.000 record is too slow

i have a requirement to make report using XLSX file , this report may contains 10.000-1.000.000 rows of trx. i made decision using phpspreadsheet from https://phpspreadsheet.readthedocs.io/en/latest/
The problem is it takes too long to write 10.000 hours of which each record consist of 50 columns. its nearly 24 hours and the script still in running and the progress is 2300/10000, here my codes :
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$client = new \Redis();
$client->connect('192.168.7.147', 6379);
$pool = new \Cache\Adapter\Redis\RedisCachePool($client);
$simpleCache = new \Cache\Bridge\SimpleCache\SimpleCacheBridge($pool);
\PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache);
$process_time = microtime(true);
if(!file_exists('test.xlsx')) {
$spreadsheet = new Spreadsheet();
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("test.xlsx");
unset($writer);
}
for($r=1;$r<=10000;$r++) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("test.xlsx");
$rowArray=[];
for($c=1;$c<=50;$c++) {
$rowArray[]=$r.".Content ".$c;
}
$spreadsheet->getActiveSheet()->fromArray(
$rowArray,
NULL,
'A'.$r
);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("test.xlsx");
unset($reader);
unset($writer);
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
}
$process_time = microtime(true) - $process_time;
echo $process_time."\n";
notes :
i propose CSV file but the clients only wants XLSX
without redis cache it gives memory error even only <400 record
im not intended to read .XLSX using php, only write action. looks like the library reads the entire spreadsheet
at example above it takes open-close file every 1 record file, when im doing open->writeall->close its shows memory error at the mid progress
at example above it takes open-close file every 1 record file, when im
doing open->writeall->close its shows memory error at the mid progress
I see you are opening (createReader) and saving (createWriter) each time when filling content in the loop. It may be the cause of slowing down the process. From your logic, eventually you are writing back the content to the same file, so it can just be open-one-time > write 50x10k records > close-and-save.
A quick test with re-arrange your coding as follows, which result in approximately 25 seconds using my local Xampp in Windows. I'm not sure if this meets your requirement or not, but I think it may consume more time if the content is some long string. My guess is that if you run on a powerful server, the performance might get significant improve in time wise.
$process_time = microtime(true);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($file_loc);
$row_count = 10000;
$col_count = 50;
for ($r = 1; $r <= $row_count; $r++) {
$rowArray = [];
for ($c = 1; $c <= $col_count; $c++) {
$rowArray[] = $r . ".Content " . $c;
}
$spreadsheet->getActiveSheet()->fromArray(
$rowArray,
NULL,
'A' . $r
);
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($target_dir . 'result_' . $file_name);
unset($reader);
unset($writer);
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
$process_time = microtime(true) - $process_time;
echo $process_time."\n";
Edited:
without redis cache it gives memory error even only <400 record
My quick test is without any cache settings. My guess for the memory issue is that you are opening the XLSX file every time you write the content for each row and then saving it back to the original file.
Every time you open the XLSX file, memory will be loaded and cached with all PhpSpreadsheet object info as well as the [previous content + (50 new columns added each time after saving)] and the memory grows in an exponential way; can you imagine that?
Finally, the memory clearing is way slower and results in memory errors.
1st time open and save
-> open: none
-> save: row A, 50 cols
2nd time open and save
-> open: row A, 50 cols
-> save: row A, 50 cols, row B, 50 cols
3nd time open and save
-> open: row A, 50 cols, row B, 50 cols
-> save: row A, 50 cols, row B, 50 cols, row C, 50 cols
so on and so forth...
memory might still keeping your previous loaded cache
and not releasing so fast (no idea how server is handling the memory, Orz)
and finally memory explode ~ oh no

Find out page numbers of Ppt files with php

I want to get the page numbers of ppt files.
My Code:
$filename = "aaa.ppt";
$word = new COM("Powerpoint.Application");
$word->PresentationDocument->Open($filename);
$wdStatisticPages = 2; // Value that corresponds to the Page count in the Statistics
echo $word->ActivePresentation->SlideParts->Count($wdStatisticPages);
$word->ActivePresentation->Close();
$word->Quit();
But it gives error:
Fatal error: Uncaught exception 'com_exception' with message 'Unable
to lookup `PresentationDocument': Unknown name. '
This is kind of issue is due to the following factors.
PHP.ini settings
File/ Folder Permission
allow open is not enabled in the server
allowed upload size
I got the answer, thanks Suyog for your efforts.
$filename = "aaa.ppt";
$power = new COM("Powerpoint.Application");
$power->visible = True;
$power->Presentations->Open(realpath($filename));
echo $power->ActivePresentation->Slides->Count;
//$word->ActiveDocument->PrintOut();
$power->ActivePresentation->Close();
$power->Quit();

PHP Fatal error: Undefined class constant 'WRITE_SCOPE'

I use PHP app in Google appengine,
I'm trying to read a input file and write it to an output file in Storage bucket like below.
$input_file = fopen('gs://mybucket/input.csv','r');
$output_file = fopen('gs://mybucket/output.csv', 'w');
And trying to write some date like
while(!feof($input_file)) {
$csv = fgetcsv($input_file,1024);
if(!$csv[0]){
fclose($output_file); //Close the connection when the loop ends
fclose($input_file);
exit(0);
}
fwrite($output_file, $csv[0]."\r\n");
}
It works perfectly, When i try to upload some data in to input file and it successfully write in to output.csv as well. but if i try more than 5 or 6th time it starts to throw an error in appengine logs like below. Any help to troubleshoot this issue will be highly appreciated!
2015-04-08 21:31:29.006 PHP Fatal error: Undefined class constant 'WRITE_SCOPE' in /base/data/home/runtimes/php/sdk/google/appengine/ext/cloud_storage_streams/CloudStorageWriteClient.php on line 214
Update:
I think this is because of opening 2 file streams at same time,
Did some work around and solved this!
$input_file = fopen('gs://mybucket/input.csv','r');
$array_acc = array();
while(!feof($input_file)) {
$csv = fgetcsv($input_file, 1024);
if($csv[0]) array_push($array_acc, $csv[0]);
}
fclose($input_file); //close the file
$acc_count = count($array_acc);
$output_file = fopen('gs://tool-synclio/output.csv','w'); // Open the output file now
while($acc_count > 0){
fwrite($output_file,$array_acc[$acc_count]."\r\n");
$acc_count --;
}
fclose($output_file);
But, I'm still waiting for some one to give better solution.
You have 2 dollar signs in a variable:
fwrite($output_file, $$csv[0]."\r\n");

Populate Excel Range using COM object in php

I would like to speed up excel generation on server using range. I know hot to populate 1 cell one by one.
For instance:
$ExcelApp = new COM("excel.application") or die("Unable to start MS Excel");
$ExcelWordbook = $ExcelApp->Workbooks->Add();
$ExcelSheet = $ExcelWordbook->Worksheets(1);
$ExcelSheet->Activate;
$ExcelSheet->Cells(1, 1)->value = "id";
$ExcelSheet->Cells(1, 2)->value = "user";
$ExcelSheet->Cells(1, 3)->value = "pn";
But How to make it in one shot using range? Something like this of course is not working.
$ExcelSheet->Range("A1:A3")->value = array("id","user","pn");
Any idea? Thanks
Have you try with phpexcel framework its a excellent framework

OLE AUTOMATION WITH PHP or PHP-CLI?

I read here http://www.daniweb.com/code/snippet217293.html# it is possible.
What should be activated in PHP.Ini or elsewhere to make this work ? Are there any examples with Excel ?
If you are running php on windows it will be installed by default. There are a few options you can set though. I don't believe they're needed:
http://www.php.net/manual/en/com.configuration.php
As for an example working with excel? Here's a little snippet I found for pulling out a value from an excel spreadsheet:
<?PHP
$filename = "c:/spreadhseet/test.xls";
$sheet1 = 1;
$sheet2 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
print "Application name: {$excel_app->Application->value}\n" ;
print "Loaded version: {$excel_app->Application->version}\n";
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
$Worksheet = $Workbook->Worksheets($sheet2);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?>
what's your target? Import excel into a DB? You can use PHPExcelReader or dbTube.org
for this task. Here you are.
PHP is platform independend. I think it is not a good idea to break this....in
a long term of view.
greeting
NotALinuxMan

Categories