test_pdf.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. use \Mpdf\Mpdf;
  3. // Controller may be included directly and $pdf can be accessed.
  4. $test = Test::get($id);
  5. if(!$test) throw new Exception(__("Test not found."));
  6. $project = $test->getProject();
  7. $customer = $project->getCustomer();
  8. $template = $project->getTemplate();
  9. $template->setOverrides($project->getTemplateOverrides());
  10. $template_parts = json_decode($template->getJSON());
  11. $fields = array_merge($template_parts->beforeTest, $template_parts->duringTest);
  12. $field_name_label_map = [];
  13. foreach($fields as $field) {
  14. if(empty($field->name)) continue;
  15. $field = $template->applyOverridesToField($field);
  16. $field_name_label_map[$field->name] = $field->label;
  17. }
  18. if(!isset($output)) $output = true;
  19. $location = $test->getLocation();
  20. $tester = $test->getTester();
  21. $test_values = $test->getValues();
  22. $keys = array_column($test_values, "name");
  23. $values = array_column($test_values, "value");
  24. $values = array_combine($keys, $values);
  25. $values = array_filter($values, function($value) {
  26. return strlen($value) !== 0;
  27. });
  28. list($date_start, $time_start) = explode(" ", $test->dts_start);
  29. list($date_end, $time_end) = explode(" ", $test->dts_end);
  30. $dt_start = new DateTime($test->dts_start . " UTC");
  31. $dt_start->setTimezone(new DateTimeZone(TIMEZONE));
  32. $dt_end = new DateTime($test->dts_end . " UTC");
  33. $dt_end->setTimezone(new DateTimeZone(TIMEZONE));
  34. $time_start = $dt_start->format("H:i:s");
  35. $time_end = $dt_end->format("H:i:s");
  36. $diff = $dt_start->diff($dt_end, true);
  37. $date_start = date("d/m/Y", strtotime($test->dts_start . " UTC"));
  38. $duration_text = str_pad($diff->h, 2, "0", STR_PAD_LEFT) . ":" . str_pad($diff->i, 2, "0", STR_PAD_LEFT) . ":" . str_pad($diff->s, 2, "0", STR_PAD_LEFT);
  39. $location_text = " $location->name";
  40. if($location->internal_id) {
  41. $location_text .= " ({$customer->internal_id_field_name}: {$location->internal_id})";
  42. }
  43. $template_processing_file_path = $template->getProcessingFilePath("test_pdf.php");
  44. // A4 size: 210x297mm
  45. // Start PDF Building
  46. $mpdf = new Mpdf([
  47. "margin_top" => 26,
  48. "margin_bottom" => 15,
  49. "margin_left" => 15,
  50. "margin_right" => 15,
  51. ]);
  52. $mpdf->SetSubject("Test Result");
  53. $mpdf->SetCreator(APP_NAME);
  54. $mpdf->SetAuthor(APP_NAME);
  55. $mpdf->SetTitle("{$location->name} Test Result @ {$date_start}");
  56. $mpdf->useSubstitutions = true;
  57. ob_start()
  58. ?>
  59. <!DOCTYPE html>
  60. <html>
  61. <head>
  62. <style>
  63. body {
  64. font-family: sans-serif;
  65. font-size: 0.75rem;
  66. }
  67. a {
  68. color: blue;
  69. }
  70. .bl {
  71. border-bottom: 1px solid #eee;
  72. }
  73. .section {
  74. background: #e2f0d9;
  75. color: #385723;
  76. padding: 4px 8px;
  77. margin-bottom: 8px;
  78. font-size: 1.25rem;
  79. }
  80. .subheader {
  81. text-transform: uppercase;
  82. color: #888;
  83. padding: 4px 8px;
  84. background: #eee;
  85. margin-bottom: 8px;
  86. }
  87. div.field {
  88. float: left;
  89. width: 29%;
  90. font-weight: bold;
  91. }
  92. div.value {
  93. float: left;
  94. padding-left: 1%;
  95. width: 69%;
  96. }
  97. .wide-field-names div.field {
  98. width: 59%;
  99. }
  100. .wide-field-names div.value {
  101. width: 39%;
  102. }
  103. div.clear {
  104. clear: both;
  105. }
  106. .text-muted {
  107. color: #ccc;
  108. }
  109. .hr {
  110. border-bottom: 2px solid #39b549;
  111. margin-bottom: 1rem;
  112. }
  113. </style>
  114. </head>
  115. <body>
  116. <!--mpdf
  117. <htmlpageheader name="Header">
  118. <table class="header" width="100%">
  119. <tr>
  120. <td width="33%">
  121. <img src="/img/logo.png" height="50" />
  122. </td>
  123. <td width="34%" align="center">
  124. <?php
  125. if($test->retest) {
  126. ?>
  127. <img src="/img/retest.png" height="38" />
  128. <?php
  129. }
  130. ?>
  131. </td>
  132. <td width="33%" align="right">
  133. <strong color="red">Test / Location ID: <?=$test->id?> / <?=$location->id?></strong><br />
  134. Tel: 01579 342 104<br />
  135. <a href="https://check-policy.com">check-policy.com</a><br />
  136. <a href="mailto:info@check-policy.com">info@check-policy.com</a>
  137. </td>
  138. </tr>
  139. </table>
  140. </htmlpageheader>
  141. <htmlpagefooter name="Footer">
  142. <div class="hr"></div>
  143. <br />
  144. <table width="100%">
  145. <tr>
  146. <td width="33%"><strong>Check Policy Limited</strong></td>
  147. <td width="33%" align="middle">8 Market Street, Liskeard, Cornwall, PL14 3JJ</td>
  148. <td width="33%" align="right">Company Number: 09506865</td>
  149. </tr>
  150. </table>
  151. </htmlpagefooter>
  152. <sethtmlpageheader name="Header" value="on" show-this-page="1" />
  153. <sethtmlpagefooter name="Footer" value="on" />
  154. mpdf-->
  155. <?php
  156. require $template_processing_file_path;
  157. ?>
  158. </body>
  159. </html>
  160. <?php
  161. $html = ob_get_clean();
  162. $mpdf->WriteHTML($html);
  163. // Output
  164. if($output) {
  165. $mpdf->Output();
  166. }