manilla_folder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* -------------------------------------------------------------------------- */
  3. /* CONFIGURATION */
  4. /* -------------------------------------------------------------------------- */
  5. date_default_timezone_set("Australia/Hobart");
  6. error_reporting(E_ALL);
  7. ini_set('display_errors', 1);
  8. require_once 'connection.php';
  9. include_once "dompdf/vendor/autoload.php";
  10. use Dompdf\Dompdf;
  11. use Dompdf\Options;
  12. if (session_status() !== PHP_SESSION_ACTIVE) session_start();
  13. if (empty($_SESSION['csrf'])) $_SESSION['csrf'] = bin2hex(random_bytes(16));
  14. $csrf = $_SESSION['csrf'];
  15. #$enquiry_date = date("l dS M 'y");
  16. $drg = $_GET['drg'] ?? '';
  17. if (!empty($_GET['drg'])) {
  18. include "table.php";
  19. }
  20. /* New: toggle PDF mode */
  21. $asPdf = isset($_GET['pdf']);
  22. /* If we are about to generate a PDF, start buffering the HTML now */
  23. if ($asPdf) {
  24. ob_start();
  25. }
  26. ?>
  27. <!doctype html>
  28. <html lang="en">
  29. <head>
  30. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <title><?= htmlspecialchars($drg) ?> - Manilla Folder Label</title>
  33. <link rel="shortcut icon" href="images/blueprint.ico" type="image/x-icon">
  34. <!-- normal view keeps external CSS -->
  35. <?php if (!$asPdf): ?>
  36. <link href="css/bootstrap.css" rel="stylesheet">
  37. <link href="css/blueprint.css" rel="stylesheet">
  38. <link href="css/print.css" rel="stylesheet" media="print">
  39. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
  40. <?php else: ?>
  41. <style>
  42. <?php
  43. // Inline local CSS so Dompdf reads it from disk
  44. foreach (['css/bootstrap.css', 'css/blueprint.css', 'css/print.css'] as $file) {
  45. $path = __DIR__ . '/' . $file;
  46. if (is_readable($path)) {
  47. echo "/* $file */\n" . file_get_contents($path) . "\n";
  48. }
  49. }
  50. ?>
  51. /* PDF-only fallbacks for Bootstrap bits Dompdf struggles with */
  52. .container-fluid { width: 100%; padding: 0 12px; color: #000; }
  53. .row { display: block; margin: 0; }
  54. .col-md-12 { width: 100%; padding: 0; }
  55. .fw-bold { font-weight: 700; }
  56. .small { font-size: 0.875rem; }
  57. .mb-0 {margin-bottom:0cm;line-height:0.9rem;}
  58. h3 {font-size: 1.75rem;}
  59. .h4 {font-size: 1.5rem;}
  60. @page { size: 320mm 243mm; margin: 0.08cm 0.25cm 0.25cm 0.25cm; }
  61. </style>
  62. <?php endif; ?>
  63. </head>
  64. <body style="background-color:#fff !important;">
  65. <!-- Show a Download link in normal view -->
  66. <?php if (!$asPdf): ?>
  67. <div class="toolbar">
  68. <a class="btn btn-primary" href="?drg=<?= urlencode($drg) ?>&pdf=1">
  69. <i class="bi bi-file-earmark-arrow-down"></i> Download PDF
  70. </a>
  71. </div>
  72. <?php endif; ?>
  73. <div class="container-fluid">
  74. <div class="row ms-3 mx-auto p-0 plaque">
  75. <div class="plate pt-0 px-0">
  76. <div class="col-md-12 mb-5">
  77. <p><span class="fw-bold"><?= htmlspecialchars($drg) ?></span> - <?= htmlspecialchars($site_address ?? '') ?></p>
  78. </div>
  79. <div class="col-md-12 mb-0">
  80. <h3><span class="fw-bold"><?= htmlspecialchars($drg) ?></span> - <?= htmlspecialchars($joint_name ?? '') ?></h3>
  81. </div>
  82. <div class="col-md-12 ms-5 mt-3 mb-0 small">
  83. <p class="mb-0 fw-bold">CLIENT:</p>
  84. <p class="mb-0"><?= htmlspecialchars(($firstname ?? '') . ' ' . ($lastname ?? '')) ?></p>
  85. <p class="mb-0"><?= htmlspecialchars($joint_name ?? '') ?></p>
  86. <p class="mb-0"><b>MOB:</b> <?= htmlspecialchars($client_mobile ?? '') ?></p>
  87. <p class="mb-0"><b>EMAIL:</b> <?= htmlspecialchars($client_email ?? '') ?></p><br>
  88. <p class="mb-0 fw-bold">SITE DETAILS:</p>
  89. <p class="mb-0"><?= htmlspecialchars($site_address ?? '') ?></p>
  90. <p class="mb-0"><b>TITLE:</b> <?= htmlspecialchars($title_id ?? '') ?> <b>PID:</b> <?= htmlspecialchars($property_id ?? '') ?></p>
  91. <p class="mb-0"><b>Planning Zone:</b> <?= htmlspecialchars($planning_zones ?? '') ?></p>
  92. <p class="h4 mt-3 mb-0"><b>Date:</b> <?= htmlspecialchars(date('M Y',strtotime($enquiry_date)) ?? date("M Y")) ?></p>
  93. </div>
  94. </div>
  95. </div>
  96. </div>
  97. </body>
  98. </html>
  99. <?php
  100. /* If PDF requested, capture the HTML and stream a PDF */
  101. if ($asPdf) {
  102. $html = ob_get_clean();
  103. $options = new Options();
  104. $options->set('isRemoteEnabled', true); // allow CDN CSS and images
  105. $options->set('isHtml5ParserEnabled', true);
  106. $options->setChroot(__DIR__);
  107. $dompdf = new Dompdf($options);
  108. $dompdf->loadHtml($html, 'UTF-8');
  109. // Custom paper size in points. 1 mm = 2.83465 pt
  110. $MM = 2.83465;
  111. $widthPt = 320 * $MM;
  112. $heightPt = 239 * $MM;
  113. $dompdf->setPaper([0, 0, $widthPt, $heightPt], 'landscape');
  114. $dompdf->render();
  115. // Nice filename fallback if pieces are missing
  116. $file = trim(($drg ?? 'label')) . ' - Manilla Folder Label.pdf';
  117. // true => force download. set to false to open in browser
  118. $dompdf->stream($file, ['Attachment' => true]);
  119. exit;
  120. }
  121. ?>