【问题标题】:How do I set header and footer in a PDF, from HTML, with knp-snappy-bundle?如何使用 knp-snappy-bundle 在 PDF 中设置页眉和页脚?
【发布时间】:2018-05-14 23:34:40
【问题描述】:

长话短说

使用knp-snappy-bundle,我无法在 PDF 中生成页眉,但我实际上可以生成页脚。

是错误、功能还是我做错了什么?

详情

1。环境

我正在测试knp-snappy-bundle,我还从h4cc 安装了wkhtmltopdf 二进制文件。这是我composer.json的一部分:

"h4cc/wkhtmltopdf-amd64": "^0.12.3",
"knplabs/knp-snappy-bundle": "^1.5",

wkhtmltopdf 生成的二进制文件是这样说的:

$ vendor/bin/wkhtmltopdf-amd64 --version
wkhtmltopdf 0.12.3 (with patched qt)

2。 没有页眉或页脚,它可以工作

我已经设置了一个使用knp-snappy-bundle 的控制器并且它可以工作:

这是我的PdfController

public function downloadPdfAction( string $docName ) : Response
{
    $pdf = $this->getPdf( $docName );

    return new PdfResponse( $pdf, $this->getFilename( $docName ) );
}

private function getPdf( string $docName ) : string
{
    $html = $this->renderView( 'AppBundle:documents:' . $docName . '.pdf.twig' );

    $options = [];

    $generator = $this->getPdfGenerator();
    $pdf = $generator->getOutputFromHtml( $html, $options );

    return $pdf;
}

private function getPdfGenerator() : Pdf
{
    $generator = $this->get( 'knp_snappy.pdf' );

    return $generator;
}

基本上是这样的:

  • 有一个downloadPdf 操作
      1. 按名称获取 PDF 文档,作为参数传入。在本例中,我们将使用 'helloWorld'
      1. 使用捆绑包中的 PdfResponse 类返回使用 PDF 内容创建的新 Response
  • 获取 PDF
      1. 它使用twigengine 渲染视图。
      1. 获取服务(拆分成另一个函数getPdfGenerator())。
      1. 使用getOutputFromHtml() 的服务,没有传入任何选项。

这是我的helloWorld.pdf.twig

<html>
    <body>
        <div class="pdfPageBody">
            <h1>
                Hello, World!
            </h1>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
    </body>
</html>

这是生成的 PDF,完全符合预期:

3。 使用页眉和页脚,它会失败!

所以我现在添加页眉和页脚。为此,我只需添加几根树枝,将 HTML 渲染成几个变量,然后将它们传递到 Pdf 渲染器的选项中:

private function getPdf( string $docName ) : string
{
    $html = $this->renderView( 'AppBundle:documents:' . $docName . '.pdf.twig' );

    $header = $this->renderView( 'AppBundle:documents:header.pdf.twig' );
    $footer = $this->renderView( 'AppBundle:documents:footer.pdf.twig' );

    $options = [
        'header-html' => $header,
        'footer-html' => $footer,
    ];

    $generator = $this->getPdfGenerator();
    $pdf = $generator->getOutputFromHtml( $html, $options );

    return $pdf;
}

除了包含的文本之外,页眉和页脚彼此相同:

这是我的header.pdf.twig

<html>
    <body>
        <div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
            This is a header
        </div>
    </body>
</html>

还有footer.pdf.twig

<html>
    <body>
        <div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
            This is a footer
        </div>
    </body>
</html>

哇!!!页脚被渲染,但页眉没有!

这是我得到的:

图片中要注意的:

  1. 标题中有“某事”。我可以看到页面内容的文本,例如“已剪辑”。
  2. 页脚未完全呈现,因为它隐藏了所有底部边框线,并且文本基线与页面底部边缘对齐。

太好了,所以我的问题!!

  • 我应该怎么做才能使标头也呈现?
  • 不应该是我可以通过这个简单的设置看到在 PDF 中呈现的标题吗?为什么没有出现?

【问题讨论】:

    标签: pdf rendering wkhtmltopdf knp-snappy


    【解决方案1】:

    解决了!

    好吧,看来wkhtmltopdf 对文档类型的考虑确实很严格,如果不考虑它会做一些奇怪的事情。

    灵感来自:https://stackoverflow.com/a/28343079/1315009

    所以,我将所有树枝都更改为包含&lt;!DOCTYPE html&gt;

    helloWorld.pdf.twig

    <!DOCTYPE html>
    <html>
        <body>
            <div class="pdfPageBody">
                <h1>
                    Hello, World!
                </h1>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            </div>
        </body>
    </html>
    

    header.pdf.twig

    <!DOCTYPE html>
    <html>
        <body>
            <div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
                This is a header
            </div>
        </body>
    </html>
    

    footer.pdf.twig

    <!DOCTYPE html>
    <html>
        <body>
            <div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
                This is a footer
            </div>
        </body>
    </html>
    

    最终结果

    我终于明白了:

    恰好有:

    1. 标题,正文没有奇怪的剪辑
    2. 页脚。

    希望能提供帮助!

    【讨论】:

      【解决方案2】:

      即使在确定我设置了 DocType 之后,我的标题仍然存在这个问题。原来是因为我在标题中浮动了 2 个 div。删除了浮动样式并且它起作用了。 PDF 的主要内容中仍有浮动的 div,这不会导致任何问题。

      【讨论】:

        猜你喜欢
        • 2014-03-28
        • 2015-05-15
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 2015-01-31
        • 2019-06-30
        • 2012-03-11
        • 2023-01-12
        相关资源
        最近更新 更多