wkhtmltopdflaravel-snappy

How to add header and footer on every page with wkhtmltopdf unpatched qt?


I am using laravel-snappy & wkhtmltopdf 0.12.2.4 (unpatched qt) which doesn't support the --header-html switch so I have to manually create a header but the problem is the header and footer only appear on the first page. Is it possible to add a header and footer to every page without upgrading or downgrading wkhtmltopdf?

html

<div class="container">
    <div class="header">
        <img src="{{URL ('/images/logo.png')}}" alt="logo" width="125" height="50"/>
        <h2>Parts Orientation</h2>
    </div>
    <div class="row  marginBody">
        @foreach ($parts as $part)
            <div class="col-xs-2 marginThumbnail">
                <div class="border text-center">
                    <img class="img" src="data:image/jpeg;base64, {{$part->image}}"
                         alt="Error" width="120" height="120"/>
                    <p class="displayText {{$part->class}}">
                        {{$part->name}}
                    </p>
                </div>
            </div>
        @endforeach
        <div class="footer">
            <p> {{$date}} </p>
        </div>
    </div>
</div>
</body>
</html>

controller

public function printPDF(Request $request)
{
    $parts = Parts::all();
    $date = $this->getTime();

    $pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString()])
                    ->setOrientation('landscape')
                    ->setPaper('a4');
    }
}

Solution

  • I was able to figure out a solution that works for me. Its very messy but it works

    controller

    public function printPDF(Request $request)
    {
        $parts = Parts::all();
        $date = $this->getTime();
        // divide by 36 because I can fit 36 items on each page
        $totalPages = ceil(count($parts)/36);
    
        $pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString(), "totalPages" => $totalPages])
                        ->setOrientation('landscape')
                        ->setPaper('a4');
        }
    }
    

    html

    <body>
    @php
    $count = 0;
    $page = 0;
    @endphp
    <div class="container">
        <div class="header">
            <img src="{{URL ('/img/logo.png')}}" alt="logo" width="125" height="50"/>
            <h2>PARTS ORIENTATION</h2>
        </div>
        <div class="row  marginBody">
            @foreach ($parts as $part)
            @php
                // 36 is the total number of items I can fit on a page
                // so after 36 I add a header and footer
                if ($count === 36) {
    
                    echo '<div class = "newPageHeader">';
                    echo '<h2>PARTS ORIENTATION</h2>';
                    echo '</div>';
                    $count = 0;
                    $page++;
                }
            @endphp
                <div class="col-xs-2 outline">
                    <div class="border text-center">
                        <img class="img" src="data:image/jpeg;base64, {{$part->image}}"
                             alt="Error" width="127" height="127"/>
                        <p class="displayText {{$part->c,ass}}">
                            {{$part->text}}
                        </p>
                    </div>
                </div>
            @php
                $count++;
                if ($count == 36) {
                    // 36 is the total number of items I can fit on a page
                    // so after 36 I add a header and footer
                    $page = ($page == 0) ? 1 : $page;
                    echo '<footer class="footer">';
                    echo "<span class='dateFooter'> $date </span>";
                    echo "<span class='pageFooter'> $page of $totalPages </span>";
                    echo '</footer>';
              }
              elseif ($totalPages == $page) {
                    echo '<footer class="lastFooter">';
                    echo "<span class='dateFooter'> $date </span>";
                    echo "<span class='lastPageFooter'> $page of $totalPages </span>";
                    echo '</footer>';
            }
            @endphp
            @endforeach
        </div>
    </div>
    </body>