test_pdf.php 2.5 KB

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