test_pdf.php 4.7 KB

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