user3574492
user3574492

Reputation: 6435

PDF headers and footers not rendering with wkhtmltopdf

I am using laravel-snappy in my Laravel project to generate PDF files from View templates. This package uses wkhtmltopdf.

I am trying to set some headers and footers when I generate the PDF in my routes file like this:

Route::get('/statement-of-fact', function () {
       $pdf = PDF::loadView('pdf.statement-of-fact');
       $footer = resource_path('views/pdf/statement-of-fact-footer.html');
       $pdf->setOption('footer-html', $footer);
       return $pdf->stream();
    })->name('statement-of-fact');

The PDF generates fine but the footer is never rendered.

What am I doing wrong here?

My HTML file:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

  <div class="row">
    <div class="col-xs-12">

      Hello 
    </div>
  </div>
</body>
</html>

Upvotes: 2

Views: 7252

Answers (2)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

On

$pdf->setOption('footer-html', $footer);

$footer should be route to footer and not to path of footer, so it should be something like

Route::get('pdf-footer', function(){
    return view('nova::pdf-partials.footer');
})->name('pdf.footer');

and

$pdf->setOption('footer-html', route('pdf.footer'));

ref

Upvotes: 0

Artur Subotkevič
Artur Subotkevič

Reputation: 1929

You should try to load the footer this way:

$pdf = PDF::loadView('path.to.your.pdf.view');

$pdf->setOption('footer-html', view('path.to.your.footer'));

return $pdf->inline();

And in your config/snappy.php set some margins:

'pdf' => array(
    // ...
    'options' => array(
        'margin-top'    => 15,
        'margin-right'  => 10,
        'margin-bottom' => 15,
        'margin-left'   => 10,
    ),
),

Upvotes: 2

Related Questions