test_pdf.php 4.6 KB

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