php - 如何为生成的多页 pdf 添加分页符

标签 php html css laravel pdf

我创建了一个元素,我需要在其中生成多个 5.5in W x 8.5in H用于打印的卡片。我将每张卡片放在 11in W x 8.5in H 的横向页面上.所以 2 张卡片在 1 页上。 我当前的 PDF 有 77 张半页卡片,应该是 38.5 页的 PDF。

我遇到的问题是分页符的 css 属性:

page-break-after: auto | always | avoid | left | right
page-break-before: auto | always | avoid | left | right
page-break-inside: auto | avoid

Detailed descriptions of each css page break possible value:

auto: Initial value. Automatic page breaks.
always: Always force page breaks before/after the element.
avoid: Avoid page breaks before/after/inside the element.
left: Force page breaks before/after the element so that the next page is formatted as a left page.
right: Force page breaks before/after the element so that the next page is formatted as a right page.

问题

所有 77 张卡片都以一页 PDF 的形式叠放在一起。 (这张图片中有 77 张卡片叠在一起) enter image description here

我需要在放置 2 张卡片以构成 38.5 页后将它们分开。

这是我正在做的事情:我正在遍历数据库以生成所需的卡片并应用 card_one_bg 类和 card_two_bg将卡片放在页面的左侧和右侧。我通过检查奇数和偶数的卡数来做到这一点。奇数 = 左侧,偶数 = 右侧。

    @php
    $rows = 0;
    @endphp
    @foreach($haitiKids as $index =>$hk)
    @php
    $nummReceived = $hk->sponsors_received;
    $nummNeeded = $hk->sponsors_needed - $nummReceived;
    @endphp
    @for($i = 0; $i < $hk->sponsors_needed; $i++ )
        @if($nummReceived-- <= 0) @php $rows++;
    @endphp
    <div class="cutline"></div>
    <!-- Class "page" = the 11in W x 8.5in H page -->
            <div class="page">
                    <!-- CARD START: Class "card_one_bg" = left side of page | Class "card_two_bg" = right side of page -->
                    <div class="{{ ($rows % 2 === 0) ? 'card_one_bg' : 'card_two_bg'}}">
                       ...ALL CARD CONTENT
                    <!-- CARD END -->
                    @endif
                    @endfor
                </div>
            @endforeach

这是相关的 Css:

    .page {
        position: absolute;
        overflow: hidden;
        width: 11in;
        height: 8.5in;
        font-family: "Muli", sans-serif;
        top: 0;
        left: 0;
    }

    .break-left {
        page-break-after: left;
    }

    .break-right {
        page-break-after: always;
    }

    .no-break {
        page-break-inside: always;
    }

我在摆弄上面列出的一些辅助类:.break-left , .break-right , .no-break .. 没有运气

几天来我一直在做这个,做了很多试验和错误,但如果我确实让页面中断,那么一页的左侧是空白的,下一页的右侧是空白的,所以 -在。像这样: enter image description here

如果需要,我很乐意提供更多信息。任何帮助将不胜感激。

PS:使用laravel/dompdf包生成pdf。

最佳答案

您可以尝试以下代码段。

.page-break {
    page-break-after: always;
}
<!-- Class "page" = the 11in W x 8.5in H page -->
<div class="page">
    @php
      $rows = 0;
    @endphp
    @foreach($haitiKids as $index =>$hk)
     @php
      $nummReceived = $hk->sponsors_received;
      $nummNeeded = $hk->sponsors_needed - $nummReceived;
     @endphp
     @for($i = 0; $i < $hk->sponsors_needed; $i++ )
      @if($nummReceived-- <= 0) 
       @php $rows++; @endphp
                    <!-- CARD START: Class "card_one_bg" = left side of page | Class "card_two_bg" = right side of page --> <!-- you may not need these clases for left and right because page break will be inserted as a div between pages -->
                    
                    @php if($rows % 2 ===0) { 
                      echo '</div><!-- end .page-->';
                      echo '
                        <div class="cutline"></div><!-- cut line if you need this - maybe make the save div add cut line and page break-->';
                      echo '<div class="page-break"></div><!-- end .page-->';
                      echo '<div class="page"><!-- new .page-->';
                      }
                    @endphp
                    <div class="{{ ($rows % 2 === 0) ? 'card_one_bg' : 'card_two_bg'}}">
                       ...ALL CARD CONTENT
                    <!-- CARD END -->
                    @endif
                    @endfor
            @endforeach
                </div>

关于php - 如何为生成的多页 pdf 添加分页符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566360/

相关文章:

php - 是否可以在没有用户 'seeing it' 的情况下将变量传递给 php?

html - css 类不影响

javascript - 悬停时的 CSS Matrix3D 变换

html - CSS Transitions 不适用于导航

php - 具有绑定(bind)参数的 PDO sql 查询 If 更新中的语句

php - MYSQL:有条件地向表中添加和更新临时列

javascript - 如何将广告集成到混合应用程序中

PHP max_input_vars

php - 如何连接两个表中的两个mysql查询

html - 嵌套固定元素的 z-index 问题和溢出 :hidden