test_pdf.php 4.7 KB

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