phpwkhtmltopdfknp-snappy

WKHTMLTOPDF (knplabs - snappy) not showing header nor footer page


I'm using wkhtmltopdf to generate my pdf files. I've left the generation alone for some time and for whatever reason it's not generating the header and footer anymore.

Things I've tried so far (will update this when more answers come in):

This is my header file:

<!DOCTYPE html>
<html>
<head>
    <title>PDF header</title>
    <style>
        html {
            display: block;
        }
        body {
            font-family: Calibri, "Segoe Ui Regular", sans-serif;
            letter-spacing: 0px;
        }
    </style>
</head>
<body style="padding-top: 30px">
    <img src="../../images/logo_extra.jpg" style="width: 100%;"/>
</body>
</html>

This is my main file:

<?php 
session_start();

require __DIR__ . '/../vendor/autoload.php';

use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');

header('Content-Type: application/pdf');
// header('Content-Disposition: attachment; filename="offerte.pdf"');

$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');

// I know there is a 'cover' function in WKHTMLTOPDF
$file = file_get_contents('pdf/cover.php');

echo $pdf->getOutputFromHtml($file); 
?>

And as always, please: Give me an explanation and maybe an example but not just a bunch of working code!

PS: If you see any other mistakes, please let me know.


Solution

  • wkhtmltopdf has an issue with header/cover/footer. I didn't dig into it very deep as adding margins did solve it for me:

    <?php 
    session_start();
    
    require __DIR__ . '/../vendor/autoload.php';
    
    use Knp\Snappy\Pdf;
    $pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
    header('Content-Type: application/pdf');
    
    //just set margins
    $pdf->setOption('margin-top', 20);
    $pdf->setOption('margin-bottom', 15);
    $pdf->setOption('margin-left', '0');
    $pdf->setOption('margin-right', '0');
    
    $pdf->setOption('header-html', 'pdf/header.html');
    $pdf->setOption('footer-html', 'pdf/footer.html');
    $pdf->setOption('load-error-handling','ignore');
    
    $file = file_get_contents('pdf/cover.php');
    echo $pdf->getOutputFromHtml($file); 
    ?>
    

    Description

    Second problem is weird - nonexisting filename should throw an error. Comment out header and try then with false filename, snappys AbstractGenerator should say something...