reports_presentation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. ->setWidth(200)
  18. ->setOffsetX(380)
  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("/tmp", "ppt");
  41. $name .= ".png";
  42. $handle = fopen($name, "w");
  43. fwrite($handle, base64_decode($png_b64));
  44. fclose($handle);
  45. $chart_offset_y = 100;
  46. if($chart->print) {
  47. $chart_offset_y = 10;
  48. }
  49. $shape = $slide->createDrawingShape();
  50. $shape->setName("Chart")
  51. ->setDescription("Chart")
  52. ->setPath($name)
  53. ->setHeight(520)
  54. ->setOffsetX(133)
  55. ->setOffsetY($chart_offset_y);
  56. if($chart->print) {
  57. // Table
  58. $shape = $slide->createTableShape($column_count)
  59. ->setWidth(900)
  60. ->setOffsetX(30)
  61. ->setOffsetY(540);
  62. // Create header row
  63. $row = $shape->createRow();
  64. $row->setHeight(20);
  65. $cell = $row->nextCell();
  66. $labels = $chart->getPrintLabels() ?? $chart->getLabels();
  67. foreach($labels as $label) {
  68. $cell = $row->nextCell();
  69. $cell->getActiveParagraph()->getAlignment()
  70. ->setMarginBottom(4)
  71. ->setMarginLeft(4)
  72. ->setMarginRight(4)
  73. ->setMarginTop(4);
  74. $cell->createTextRun($label)
  75. ->getFont()->setBold(true);
  76. }
  77. $suffixes = $chart->getPrintSuffixes() ?? $chart->getDatalabelSuffixes();
  78. // Datasets
  79. foreach($datasets as $index => $dataset) {
  80. $label = $dataset->label;
  81. $data = $dataset->data;
  82. $suffix = $suffixes[$index] ?? "";
  83. $row = $shape->createRow();
  84. $row->setHeight(20);
  85. $cell = $row->nextCell();
  86. $cell->getActiveParagraph()->getAlignment()
  87. ->setMarginBottom(4)
  88. ->setMarginLeft(4)
  89. ->setMarginRight(4)
  90. ->setMarginTop(4);
  91. $cell->createTextRun($label)
  92. ->getFont()->setBold(true);
  93. foreach($data as $value) {
  94. $cell = $row->nextCell();
  95. $cell->getActiveParagraph()->getAlignment()
  96. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  97. ->setMarginBottom(4)
  98. ->setMarginLeft(4)
  99. ->setMarginRight(4)
  100. ->setMarginTop(4);
  101. $cell->createTextRun($value . $suffix);
  102. }
  103. if($chart->printPercentages) {
  104. $total = array_sum($data);
  105. $percentages = [];
  106. foreach($data as $value) {
  107. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  108. }
  109. $row = $shape->createRow();
  110. $row->setHeight(20);
  111. $cell = $row->nextCell();
  112. $cell->getActiveParagraph()->getAlignment()
  113. ->setMarginBottom(4)
  114. ->setMarginLeft(4)
  115. ->setMarginRight(4)
  116. ->setMarginTop(4);
  117. $cell->createTextRun("Percentage")
  118. ->getFont()->setBold(true);
  119. foreach($percentages as $string) {
  120. $cell = $row->nextCell();
  121. $cell->getActiveParagraph()->getAlignment()
  122. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  123. ->setMarginBottom(4)
  124. ->setMarginLeft(4)
  125. ->setMarginRight(4)
  126. ->setMarginTop(4);
  127. $cell->createTextRun($string);
  128. }
  129. }
  130. }
  131. }
  132. $names[] = $name;
  133. }
  134. if($action == "export-pptx") {
  135. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  136. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  137. $pptx_writer->save("php://output");
  138. }
  139. if($action == "export-odp") {
  140. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  141. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  142. $odp_writer->save("php://output");
  143. }
  144. foreach($names as $name) {
  145. @unlink($name);
  146. }
  147. exit;
  148. ?>