WKHTMLTOPDF (knplas - snappy) 不显示页眉页脚页

问题描述:

我正在使用 wkhtmltopdf 来生成我的 pdf 文件.我已经让这一代单独存在一段时间了,无论出于何种原因,它都不再生成页眉和页脚了.

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):

  • 使用 doctypehtmlheadbody 标签来标记页眉和页脚
  • 将页眉和页脚的路径更改为绝对路径(即使使用从驱动器前进的完整绝对路径 C:/xampp... 也不起作用.)可能值得指出的是,将文件名更改为不存在的东西不会引发错误.所以我不知道它是否找到了这些文件.也许有人可以告诉我一个测试这个的好方法?
  • Having the doctype, html, head and body tags ion the header and footer
  • Chaning the paths to the header and footer to absolute paths (Even using complete absolute paths from the drive forward C:/xampp... does not work.) It might be worth pointing out that changing the filename to something that doesn't exist does not throw an error. So I don't know if it finds the files. Maybe someone can tell me a good way to test this?

这是我的头文件:

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

这是我的主文件:

<?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); 
?>

和往常一样,请:给我一个解释,也许是一个例子,但不仅仅是一堆工作代码!

PS:如果您发现任何其他错误,请告诉我.

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

wkhtmltopdf 有页眉/封面/页脚问题.我没有深入研究它,因为添加边距确实为我解决了这个问题:

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); 
?>

说明

第二个问题很奇怪 - 不存在的文件名应该抛出错误.注释掉标题,然后尝试使用错误的文件名,snappys AbstractGenerator 应该会说些什么...

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