<?php
$maxTime = 30 ; // in seconds
$sleepTime = 250; // in milliseconds
echo "buh";
$ifname='C://test.xls';
$PDFCreator = New COM("PDFCreator.clsPDFCreator") or die("Couldn\\' start the COM-object");
$PDFCreator->cStart("/NoProcessingAtStartup");
$PDFCreator->cOptions->UseAutosave = 1;
$PDFCreator->cOptions->UseAutosaveDirectory = 1;
$PDFCreator->cOptions->AutosaveFormat = 0;
$DefaultPrinter = $PDFCreator->cDefaultprinter;
$PDFCreator->cDefaultprinter = "PDFCreator";
$PDFCreator->cClearcache();
if (!file_exists($ifname))
{
echo "Can't find the file: " .$ifname;
break;
}
if (!$PDFCreator->cIsPrintable($ifname))
{
echo "Converting: " .$ifname ."\\r\
\\r\
An error is occured: File is not printable!";
break;
}
$ReadyState = 0;
$PDFCreator->cOptions->AutosaveDirectory = realpath($ifname);
$PDFCreator->cOptions->AutosaveFilename = basename("C:\\test.pdf");
$PDFCreator->cPrintfile($ifname); //C'est cette ligne qui bloque
$PDFCreator->cPrinterStop = false;
// Wait 1000 miliseconds..
// This is a specific PHP-function, so you'll have to
// change this one for something else
com_message_pump(1000);
// Set the default printer back to the old one
//$PDFC->cDefaultprinter = $DefaultPrinter;
// Clear cache (again don't know why :) )
$PDFCreator->cClearcache();
// ...wait
com_message_pump(200);
// Close the printer
$PDFCreator->cClose();
?>
I had problems getting PHP to work with the COM object as well. After looking at other examples in the COM folder (Python\\Testpage2PDF.py) I tried the cSaveOptions method and it worked. Here is my example. I got it to work running PHP at the command line and tested it with an XLS and a TXT input file.
<?php $ifname='C:\\\\test.xls'; $PDFCreator = New COM("PDFCreator.clsPDFCreator") or die("Couldn\\' start the COM-object"); $PDFCreator->cStart("/NoProcessingAtStartup");
$options = $PDFCreator->cOptions; $options->UseAutosave = 1; $options->UseAutosaveDirectory = 1; $options->AutosaveFormat = 0; $options->AutosaveDirectory = "c:\\\\"; $options->AutosaveFilename = "test.pdf"; $PDFCreator->cOptions = $options; $PDFCreator->cSaveOptions(); //for testing. Shows you that $PDFCreator has the options you set //$PDFCreator->cSaveOptionsToFile('c:\\\\PDFOptions.ini');
// Wait 1000 miliseconds.. // This is a specific PHP-function, so you'll have to // change this one for something else com_message_pump(1000);
// Set the default printer back to the old one //$PDFC->cDefaultprinter = $DefaultPrinter; // Clear cache (again don't know why :) ) $PDFCreator->cClearcache(); // ...wait com_message_pump(200); // Close the printer $PDFCreator->cClose();
Thanks ChopStickr! I use your code successfully created pdf file from TXT file. But I encounter a problem with other file formats :-(
If the file was a ".doc" or ".xls", the pdfCreator will init a winword.exe or an excel.exe process and then occupied 100% cpu, and then stop to resonpse...The php script will hopeless run until it exceed the max_execution_time.
I have no idea about this. I have Office 2003 in my computer and I am using pdfCreator 0.9.9.