');
define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
define('IS_INDEX', SCRIPT_FILENAME == 'index');
Autoloader::register();
Settings::loadConfig();
// Set writers
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf');
// Set PDF renderer
if (null === Settings::getPdfRendererPath()) {
$writers['PDF'] = null;
}
// Return to the caller script when runs by CLI
if (CLI) {
return;
}
// Set titles and names
$pageHeading = str_replace('_', ' ', SCRIPT_FILENAME);
$pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - ";
$pageTitle .= 'PHPWord';
$pageHeading = IS_INDEX ? '' : "
{$pageHeading}
";
// Populate samples
$files = '';
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if (preg_match('/^Sample_\d+_/', $file)) {
$name = str_replace('_', ' ', preg_replace('/(Sample_|\.php)/', '', $file));
$files .= "{$name}";
}
}
closedir($handle);
}
/**
* Write documents
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @param string $filename
* @param array $writers
*
* @return string
*/
function write($phpWord, $filename, $writers)
{
$result = '';
// Write documents
foreach ($writers as $format => $extension) {
$result .= date('H:i:s') . " Write to {$format} format";
if (null !== $extension) {
$targetFile = __DIR__ . "/results/{$filename}.{$extension}";
$phpWord->save($targetFile, $format);
} else {
$result .= ' ... NOT DONE!';
}
$result .= EOL;
}
$result .= getEndingNotes($writers);
return $result;
}
/**
* Get ending notes
*
* @param array $writers
*
* @return string
*/
function getEndingNotes($writers)
{
$result = '';
// Do not show execution time for index
if (!IS_INDEX) {
$result .= date('H:i:s') . " Done writing file(s)" . EOL;
$result .= date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB" . EOL;
}
// Return
if (CLI) {
$result .= 'The results are stored in the "results" subdirectory.' . EOL;
} else {
if (!IS_INDEX) {
$types = array_values($writers);
$result .= '
';
$result .= 'Results: ';
foreach ($types as $type) {
if (!is_null($type)) {
$resultFile = 'results/' . SCRIPT_FILENAME . '.' . $type;
if (file_exists($resultFile)) {
$result .= "{$type} ";
}
}
}
$result .= '
';
}
}
return $result;
}
?>