1

wkhtmltopdf用来生成我的pdf文件。我已经将这一代人单独留下了一段时间,无论出于何种原因,它不再生成页眉和页脚。

到目前为止我尝试过的事情(当更多答案出现时会更新):

  • 在页眉和页脚中添加 ,doctype和标签htmlheadbody
  • 将页眉和页脚的路径更改为绝对路径(即使使用来自驱动器前向 C:/xampp... 的完整绝对路径也不起作用。)可能值得指出的是,将文件名更改为不存在的文件名不会抛出错误。所以我不知道它是否找到文件。也许有人可以告诉我一个测试这个的好方法?

这是我的头文件

<!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:如果您发现任何其他错误,请告诉我。

4

1 回答 1

1

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

<?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 应该说些什么......

于 2018-10-30T21:39:02.047 回答