Repeated content · 6 min read

Headers & Footers

Add repeating page headers and footers to HTML renders with local Blade views or raw HTML. Cloud template renders do not accept per-request header or footer overrides; put that layout inside the published cloud template instead.

Header and footer views

For fromView() and fromHtml(), use withHeader() and withFooter() to render local Laravel views as the repeating page chrome.

Controller
php
 1return BladePDF::fromView('pdf.invoice', [
 2    'invoice' => $invoice,
 3])
 4    ->withHeader('pdf.partials.header', ['invoice' => $invoice])
 5    ->withFooter('pdf.partials.footer', ['invoice' => $invoice])
 6    ->margins(90, 32, 70, 32)
 7    ->render()
 8    ->download('invoice.pdf');

Raw header and footer HTML

Use withHeaderHtml() and withFooterHtml() when the header/footer markup is generated dynamically.

Raw HTML
php
 1return BladePDF::fromHtml($html)
 2    ->withHeaderHtml('<div style="font-size:10px;width:100%;text-align:center">Acme Inc.</div>')
 3    ->withFooterHtml('<div style="font-size:10px;width:100%;text-align:center">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>')
 4    ->margins(70, 24, 70, 24)
 5    ->render()
 6    ->response('report.pdf');

Page numbers

Header and footer templates may use reserved spans for generated values. The most common ones are pageNumber and totalPages.

resources/views/pdf/partials/footer.blade.php
blade
 1<div style="font-size:10px;width:100%;padding:0 24px;color:#64748b">
 2    <span>{{ $invoice->number }}</span>
 3    <span style="float:right">
 4        Page <span class="pageNumber"></span> of <span class="totalPages"></span>
 5    </span>
 6</div>

Reserve enough margin

Header and footer content is rendered outside the main document flow. Reserve vertical space with margins, otherwise the body can visually collide with the repeated content.

Use case Suggested margins
Small footer only ->margins(32, 24, 56, 24)
Header and footer ->margins(80, 32, 70, 32)
Letterhead style ->margins(110, 40, 80, 40)

Cloud template layouts

For fromTemplate(), header and footer overrides are rejected. Put the repeated layout in the cloud template itself, usually through @extends, @section, and normal print CSS.

Cloud template
blade
 1@extends('layouts.invoice')
 2
 3@section('content')
 4    <h1>Invoice {{ $invoice['number'] }}</h1>
 5@endsection
Not supported with cloud templates

fromTemplate(...)->withHeaderHtml(...), withHeader(), withFooterHtml(), and withFooter() throw an invalid render configuration error.

Practical rules

  • Keep header and footer markup small and mostly inline-styled.
  • Set enough top and bottom margin for the repeated content.
  • Use the document body for complex first-page-only design.
  • Use cloud template layouts for dashboard-managed templates.
Was this page helpful?
Let us know if something's missing or unclear.