1
0

performance_presentation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. use PhpOffice\PhpPresentation\PhpPresentation;
  3. use PhpOffice\PhpPresentation\IOFactory;
  4. use PhpOffice\PhpPresentation\Style\Color;
  5. use PhpOffice\PhpPresentation\Style\Alignment;
  6. use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
  7. $presentation = new PhpPresentation(); // 960x720
  8. // Draw title slide
  9. {
  10. // Create slide
  11. $slide = $presentation->getActiveSlide();
  12. // Create a shape (drawing)
  13. $shape = $slide->createDrawingShape();
  14. $shape->setName("Check Policy logo")
  15. ->setDescription("Check Policy logo")
  16. ->setPath("./public/img/logo.png")
  17. ->setHeight(40)
  18. ->setOffsetX(10)
  19. ->setOffsetY(10);
  20. // Title
  21. $shape = $slide->createRichTextShape()
  22. ->setHeight(120)
  23. ->setWidth(940)
  24. ->setOffsetX(10)
  25. ->setOffsetY(315);
  26. $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
  27. $run = $shape->createTextRun($customer->name . " " . date("F Y") . " Tests");
  28. $run->getFont()->setBold(true)
  29. ->setSize(45);
  30. }
  31. $names = [];
  32. $charts_b64 = http::value("charts_b64");
  33. foreach($charts_b64 as $index => $b64) {
  34. $chart = $charts[$index];
  35. $labels = $chart->getLabels();
  36. $datasets = $chart->getDatasets();
  37. $column_count = 1 + count($labels);
  38. $slide = $presentation->createSlide();
  39. list($ignore, $png_b64) = explode("base64,", $b64);
  40. $name = tempnam("./", "ppt");
  41. $handle = fopen($name, "w");
  42. fwrite($handle, base64_decode($png_b64));
  43. fclose($handle);
  44. $chart_offset_y = 100;
  45. if($chart->print) {
  46. $chart_offset_y = 10;
  47. }
  48. $shape = $slide->createDrawingShape();
  49. $shape->setName("Chart")
  50. ->setDescription("Chart")
  51. ->setPath($name)
  52. ->setHeight(520)
  53. ->setOffsetX(133)
  54. ->setOffsetY($chart_offset_y);
  55. if($chart->print) {
  56. // Table
  57. $shape = $slide->createTableShape($column_count)
  58. ->setWidth(900)
  59. ->setOffsetX(30)
  60. ->setOffsetY(540);
  61. // Create header row
  62. $row = $shape->createRow();
  63. $row->setHeight(20);
  64. $cell = $row->nextCell();
  65. $labels = $chart->getPrintLabels() ?? $chart->getLabels();
  66. foreach($labels as $label) {
  67. $cell = $row->nextCell();
  68. $cell->getActiveParagraph()->getAlignment()
  69. ->setMarginBottom(4)
  70. ->setMarginLeft(4)
  71. ->setMarginRight(4)
  72. ->setMarginTop(4);
  73. $cell->createTextRun($label)
  74. ->getFont()->setBold(true);
  75. }
  76. $suffixes = $chart->getPrintSuffixes() ?? $chart->getDatalabelSuffixes();
  77. // Datasets
  78. foreach($datasets as $index => $dataset) {
  79. $label = $dataset->label;
  80. $data = $dataset->data;
  81. $suffix = $suffixes[$index] ?? "";
  82. $row = $shape->createRow();
  83. $row->setHeight(20);
  84. $cell = $row->nextCell();
  85. $cell->getActiveParagraph()->getAlignment()
  86. ->setMarginBottom(4)
  87. ->setMarginLeft(4)
  88. ->setMarginRight(4)
  89. ->setMarginTop(4);
  90. $cell->createTextRun($label)
  91. ->getFont()->setBold(true);
  92. foreach($data as $value) {
  93. $cell = $row->nextCell();
  94. $cell->getActiveParagraph()->getAlignment()
  95. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  96. ->setMarginBottom(4)
  97. ->setMarginLeft(4)
  98. ->setMarginRight(4)
  99. ->setMarginTop(4);
  100. $cell->createTextRun($value . $suffix);
  101. }
  102. if($chart->printPercentages) {
  103. $total = array_sum($data);
  104. $percentages = [];
  105. foreach($data as $value) {
  106. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  107. }
  108. $row = $shape->createRow();
  109. $row->setHeight(20);
  110. $cell = $row->nextCell();
  111. $cell->getActiveParagraph()->getAlignment()
  112. ->setMarginBottom(4)
  113. ->setMarginLeft(4)
  114. ->setMarginRight(4)
  115. ->setMarginTop(4);
  116. $cell->createTextRun("Percentage")
  117. ->getFont()->setBold(true);
  118. foreach($percentages as $string) {
  119. $cell = $row->nextCell();
  120. $cell->getActiveParagraph()->getAlignment()
  121. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  122. ->setMarginBottom(4)
  123. ->setMarginLeft(4)
  124. ->setMarginRight(4)
  125. ->setMarginTop(4);
  126. $cell->createTextRun($string);
  127. }
  128. }
  129. }
  130. }
  131. $names[] = $name;
  132. }
  133. if($action == "export-pptx") {
  134. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  135. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  136. $pptx_writer->save("php://output");
  137. }
  138. if($action == "export-odp") {
  139. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  140. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  141. $odp_writer->save("php://output");
  142. }
  143. foreach($names as $name) {
  144. @unlink($name);
  145. }
  146. exit;
  147. ?>