You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Laravel中使用Dompdf生成PDF时H2与Table元素重叠问题求助

Fixing H2 and Table Overlap in Dompdf (Laravel)

Hey there! Let's get that overlapping issue sorted out. The main problem here is invalid HTML structure, plus a couple of CSS quirks that are throwing off Dompdf's rendering. Here's how to fix it step by step:

1. Fix Invalid HTML Structure

You’ve placed your <h2> directly inside the <table> tag, which isn’t allowed in HTML. Tables can only contain table-specific elements like <thead>, <tbody>, <tr>, <th>, and <td>—placing an <h2> inside breaks the layout, which Dompdf struggles to render correctly.

Move the <h2> outside the table, into your .pdfContainer div instead:

<div class="pdfContainer">
  <h2 style='margin: 2rem auto; text-align: center;'>{{$studentName}}</h2>
  <table>
    <tr>
      <th> PRÉSENCE </th>
      <th> COMPORTEMENT </th>
      <th> PROGRES </th>
      <th> DATE DE L’ATELIER </th>
    </tr>
    @foreach($records as $record)
    <tr>
      <td>
        <p class="attdStatusTd{{$record['att_status']}}"> {{$record['att_status']}} </p>
      </td>
      <td>
        <p class="attBehaviour{{$record['behaviour']}}"> {{$record['behaviour']}} </p>
      </td>
      <td>
        <p class="attProgress{{$record['progress']}}"> {{$record['progress']}} </p>
      </td>
      <td>
        <p> {{$record['customDate']}} </p>
      </td>
    </tr>
    @endforeach
  </table>
</div>

2. Clean Up CSS for Consistent Spacing

Your current CSS has a few issues that contribute to the overlap:

  • float: bottom; on the table is invalid (float only accepts left, right, or none—remove this entirely)
  • The .pdfContainer uses align-items: flex-start with no flex direction set, which can cause elements to stack incorrectly
  • Missing spacing between the heading and table

Update your CSS to fix these:

.pdfContainer {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column; /* Force vertical stacking of h2 and table */
  align-items: center;
  box-sizing: border-box;
  padding: 2rem; /* Add padding to prevent content from touching the page edges */
}

table {
  width: 70%;
  text-align: center;
  margin: 1rem auto 0; /* Add top margin to separate from the h2 */
  border-collapse: collapse; /* Move this here (it belongs on the table, not cells) */
  border: none;
}

th, td {
  padding: 15px;
  border: 1px solid #252a34 !important;
  border-spacing: 0;
}

/* Keep your existing status/behaviour/progress styles as-is */
.attdStatusTdpresent {
  color: #18a14f;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-weight: 600;
  font-size: 0.95em;
  font-family: "Merriweather Sans", sans-serif;
}
.attdStatusTdabsent {
  color: #b82348;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-weight: 600;
  font-size: 0.95em;
  font-family: "Merriweather Sans", sans-serif;
}
.attdStatusTdlate {
  color: #ff2e63;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attBehaviourExcellent {
  color: #289672;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attBehaviourGood {
  color: #00bfa6;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attBehaviourAverage {
  color: #ffc947;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attBehaviourBad {
  color: #ff2e63;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attProgressExcellent {
  color: #289672;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attProgressGood {
  color: #00bfa6;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}
.attProgressNeed {
  color: #ffc947;
  padding: 8px;
  width: 140px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
  white-space: nowrap;
}
.attProgressprogressive {
  color: #185adb;
  padding: 8px;
  width: 110px;
  border-radius: 40px;
  margin: auto;
  text-transform: capitalize;
  font-size: 0.95em;
}

3. Optional: Dompdf Rendering Tweaks

If you still see minor layout issues, double-check your Dompdf settings to ensure proper paper size and rendering:

$pdf = App::make('dompdf.wrapper');
$pdf->loadView('recordsPdf', compact('records', 'studentName'))
    ->setPaper('A4', 'portrait') // Explicitly set paper size and orientation
    ->setOptions(['isHtml5ParserEnabled' => true]); // Enable HTML5 parsing for better compatibility

return $pdf->download('student-records.pdf');

These changes should resolve the overlap by fixing the invalid HTML structure and ensuring proper spacing between elements. Dompdf relies heavily on valid, well-structured HTML/CSS, so cleaning that up is almost always the first step for layout issues.

内容的提问来源于stack exchange,提问作者theStackUser45

火山引擎 最新活动