test_pdf.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // Controller may be included directly and $pdf can be accessed.
  3. $test = Test::get($id);
  4. if(!$test) throw new Exception("Test not found.");
  5. $project = $test->getProject();
  6. $template = $project->getTemplate();
  7. $template->setOverrides($project->getTemplateOverrides());
  8. $template_parts = json_decode($template->json);
  9. $fields = array_merge($template_parts->beforeTest, $template_parts->duringTest);
  10. $field_name_label_map = [];
  11. foreach($fields as $field) {
  12. if(empty($field->name)) continue;
  13. $field = $template->applyOverridesToField($field);
  14. $field_name_label_map[$field->name] = $field->label;
  15. }
  16. if(!isset($output)) $output = true;
  17. $location = $test->getLocation();
  18. $tester = $test->getTester();
  19. $test_values = $test->getValues();
  20. $keys = array_column($test_values, "name");
  21. $values = array_column($test_values, "value");
  22. $values = array_combine($keys, $values);
  23. $values = array_filter($values, function($value) {
  24. return strlen($value) !== 0;
  25. });
  26. $tiny_font_size = 8;
  27. $small_font_size = 10;
  28. $normal_font_size = 10;
  29. $header_font_size = 14;
  30. $cell_height = 5;
  31. $cell_margin = 2;
  32. list($date_start, $time_start) = explode(" ", $test->dts_start);
  33. list($date_end, $time_end) = explode(" ", $test->dts_end);
  34. $dt_start = new DateTime($test->dts_start . " UTC");
  35. $dt_start->setTimezone(new DateTimeZone(TIMEZONE));
  36. $dt_end = new DateTime($test->dts_end . " UTC");
  37. $dt_end->setTimezone(new DateTimeZone(TIMEZONE));
  38. $time_start = $dt_start->format("H:i:s");
  39. $time_end = $dt_end->format("H:i:s");
  40. $diff = $dt_start->diff($dt_end, true);
  41. $date_start = date("d/m/Y", strtotime($test->dts_start . " UTC"));
  42. $location_text = " $location->name";
  43. if($location->internal_id) {
  44. $location_text .= " (ID: $location->internal_id)";
  45. }
  46. $template_processing_file_path = $template->getProcessingFilePath("test_pdf.php");
  47. // A4 size: 210x297mm
  48. // Start PDF Building
  49. $pdf = new TestPDF("P", "mm", "A4");
  50. $pdf->SetSubject("Test Result");
  51. $pdf->SetCreator(APP_NAME);
  52. $pdf->SetAuthor(APP_NAME);
  53. $pdf->SetTitle($location->name . " Test Result @ $date_start");
  54. $pdf->AddPage();
  55. $pdf->SetFont("Arial");
  56. $pdf->SetDrawColor(240, 240, 240);
  57. $pdf->SetFillColor(226, 240, 217);
  58. $pdf->SetAutoPageBreak(false);
  59. // Logo
  60. $pdf->Image("public/img/logo.png", 11, 10, 60);
  61. // Right contact info
  62. $pdf->SetFont("Arial", "B", $tiny_font_size);
  63. $pdf->SetTextColor(255, 0, 0);
  64. $pdf->SetLeftMargin(120);
  65. $pdf->SetY(10);
  66. $pdf->Cell(80, 4, "Test / Location ID: $test->id / $location->id", 0, 1, "R");
  67. $pdf->SetFont("Arial", "");
  68. $pdf->SetTextColor(0, 0, 0);
  69. $pdf->Cell(80, 4, "Tel: 01579 342 104", 0, 1, "R", false, "tel:01579+342+104");
  70. $pdf->SetFont("Arial", "U");
  71. $pdf->SetTextColor(0, 128, 255);
  72. $pdf->Cell(80, 4, "check-policy.com", 0, 1, "R", false, "https://check-policy.com");
  73. $pdf->Cell(80, 4, "info@check-policy.com", 0, 1, "R");
  74. $pdf->SetLeftMargin(10);
  75. $pdf->SetXY(10, $pdf->GetY() + $cell_margin);
  76. require $template_processing_file_path;
  77. // Output
  78. if($output) {
  79. $pdf->Output();
  80. }
  81. ?>