如何将分页符添加到生成的多页pdf

时间:2019-08-20 03:28:30

标签: php html css laravel pdf

我创建了一个项目,需要在其中生成多张5.5in W x 8.5in H卡进行打印。我将每张卡片放在11in W x 8.5in H的横向页面上。因此,每页2张卡片。 我当前的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张卡都以1页PDF的形式并排放置。 (此图中彼此上方有77张卡enter image description here

在放置2张卡片组成38.5页之后,我需要它们打破。

这是我正在做的事情:我正在遍历数据库以生成所需的卡片,并应用card_one_bgcard_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。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码段。

.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>