No Description

Sample_Header.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. require_once __DIR__ . '/../src/PhpWord/Autoloader.php';
  3. date_default_timezone_set('UTC');
  4. /**
  5. * Header file
  6. */
  7. use PhpOffice\PhpWord\Autoloader;
  8. use PhpOffice\PhpWord\Settings;
  9. error_reporting(E_ALL);
  10. define('CLI', (PHP_SAPI == 'cli') ? true : false);
  11. define('EOL', CLI ? PHP_EOL : '<br />');
  12. define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
  13. define('IS_INDEX', SCRIPT_FILENAME == 'index');
  14. Autoloader::register();
  15. Settings::loadConfig();
  16. // Set writers
  17. $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf');
  18. // Set PDF renderer
  19. if (null === Settings::getPdfRendererPath()) {
  20. $writers['PDF'] = null;
  21. }
  22. // Return to the caller script when runs by CLI
  23. if (CLI) {
  24. return;
  25. }
  26. // Set titles and names
  27. $pageHeading = str_replace('_', ' ', SCRIPT_FILENAME);
  28. $pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - ";
  29. $pageTitle .= 'PHPWord';
  30. $pageHeading = IS_INDEX ? '' : "<h1>{$pageHeading}</h1>";
  31. // Populate samples
  32. $files = '';
  33. if ($handle = opendir('.')) {
  34. while (false !== ($file = readdir($handle))) {
  35. if (preg_match('/^Sample_\d+_/', $file)) {
  36. $name = str_replace('_', ' ', preg_replace('/(Sample_|\.php)/', '', $file));
  37. $files .= "<li><a href='{$file}'>{$name}</a></li>";
  38. }
  39. }
  40. closedir($handle);
  41. }
  42. /**
  43. * Write documents
  44. *
  45. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  46. * @param string $filename
  47. * @param array $writers
  48. *
  49. * @return string
  50. */
  51. function write($phpWord, $filename, $writers)
  52. {
  53. $result = '';
  54. // Write documents
  55. foreach ($writers as $format => $extension) {
  56. $result .= date('H:i:s') . " Write to {$format} format";
  57. if (null !== $extension) {
  58. $targetFile = __DIR__ . "/results/{$filename}.{$extension}";
  59. $phpWord->save($targetFile, $format);
  60. } else {
  61. $result .= ' ... NOT DONE!';
  62. }
  63. $result .= EOL;
  64. }
  65. $result .= getEndingNotes($writers);
  66. return $result;
  67. }
  68. /**
  69. * Get ending notes
  70. *
  71. * @param array $writers
  72. *
  73. * @return string
  74. */
  75. function getEndingNotes($writers)
  76. {
  77. $result = '';
  78. // Do not show execution time for index
  79. if (!IS_INDEX) {
  80. $result .= date('H:i:s') . " Done writing file(s)" . EOL;
  81. $result .= date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB" . EOL;
  82. }
  83. // Return
  84. if (CLI) {
  85. $result .= 'The results are stored in the "results" subdirectory.' . EOL;
  86. } else {
  87. if (!IS_INDEX) {
  88. $types = array_values($writers);
  89. $result .= '<p>&nbsp;</p>';
  90. $result .= '<p>Results: ';
  91. foreach ($types as $type) {
  92. if (!is_null($type)) {
  93. $resultFile = 'results/' . SCRIPT_FILENAME . '.' . $type;
  94. if (file_exists($resultFile)) {
  95. $result .= "<a href='{$resultFile}' class='btn btn-primary'>{$type}</a> ";
  96. }
  97. }
  98. }
  99. $result .= '</p>';
  100. }
  101. }
  102. return $result;
  103. }
  104. ?>
  105. <title><?php echo $pageTitle; ?></title>
  106. <meta charset="utf-8">
  107. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  108. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  109. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
  110. <link rel="stylesheet" href="bootstrap/css/font-awesome.min.css" />
  111. <link rel="stylesheet" href="bootstrap/css/phpword.css" />
  112. </head>
  113. <body>
  114. <div class="container">
  115. <div class="navbar navbar-default" role="navigation">
  116. <div class="container-fluid">
  117. <div class="navbar-header">
  118. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  119. <span class="sr-only">Toggle navigation</span>
  120. <span class="icon-bar"></span>
  121. <span class="icon-bar"></span>
  122. <span class="icon-bar"></span>
  123. </button>
  124. <a class="navbar-brand" href="./">PHPWord</a>
  125. </div>
  126. <div class="navbar-collapse collapse">
  127. <ul class="nav navbar-nav">
  128. <li class="dropdown active">
  129. <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-code fa-lg"></i>&nbsp;Samples<strong class="caret"></strong></a>
  130. <ul class="dropdown-menu"><?php echo $files; ?></ul>
  131. </li>
  132. </ul>
  133. <ul class="nav navbar-nav navbar-right">
  134. <li><a href="https://github.com/PHPOffice/PHPWord"><i class="fa fa-github fa-lg" title="GitHub"></i>&nbsp;</a></li>
  135. <li><a href="http://phpword.readthedocs.org/"><i class="fa fa-book fa-lg" title="Docs"></i>&nbsp;</a></li>
  136. <li><a href="http://twitter.com/PHPWord"><i class="fa fa-twitter fa-lg" title="Twitter"></i>&nbsp;</a></li>
  137. </ul>
  138. </div>
  139. </div>
  140. </div>
  141. <?php echo $pageHeading; ?>