KnpSnappyBundle header and footer doesn't show

1.4k views Asked by At

As the title says I can't visualize the footer and header with knp_snappy.pdf, I have seen several examples and none has worked for me, I get the pdf but not the footer and header, I don't know is config problem or something else, this link below works but others but not in my code. I am using symfony 3.4 , "knplabs/knp-snappy-bundle": "^1.5" and wkhtmltopdf-0.12.2.4

Examples:
https://github.com/KnpLabs/KnpSnappyBundle/issues/195
https://github.com/KnpLabs/KnpSnappyBundle/issues/154

This is my code:
config.yml

knp_snappy:
pdf:
    enabled:    true
    binary:     /usr/bin/xvfb-run /usr/bin/wkhtmltopdf
    options:    []

pdf/report.html.twig

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>My PDF</title>
</head>
<body>
<h1>My PDF</h1>
<p>Harum trium sententiarum nulli prorsus assentior.</p>
<h1>My PDF</h1>
</body>
</html>

pdf/footer.html.twig

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Contrat Thuasne Services</title>
  <script>
    /* page counter */
    function subst() {
      var vars={};
      var x=document.location.search.substring(1).split('&');
      for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
      var x=['topage','page'];
      for (var i in x) {
        var y = document.getElementsByClassName(x[i]);
        for (var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
      }
    }
  </script>
</head>
<body onload="subst()">
<footer>
  <table>
    <tr>
      <td style="width: 33%;">Test</td>
      <td style="width: 34%;" class="center"><span class="page"></span>/<span class="topage"></span></td>
      <td style="width: 33%;" class="right">&copy; Society {{ "now"|date("Y") }}</td>
    </tr>
  </table>
</footer>
</body>
</html>

controler

public function testPDFAction()
  {
    $snappy = $this->get('knp_snappy.pdf');
    $content = $this->renderView( 'pdf/report.html.twig' );

    $footer = $this->renderView( 'pdf/footer.html.twig' );
    //$snappy->setOption('header-html', $header);
    $snappy->setOption('footer-html', $footer);


    $html = $snappy->getOutputFromHtml($content);

    return new Response($html,200, [
      'Content-Type'          => 'application/pdf',
      'Content-Disposition'   => 'inline; filename="test.pdf"'
     ]
    );

    // Others test i make

 /*$html = $snappy->getOutputFromHtml($content,
 [
  // 'images' => true,
  'enable-javascript' => true,
  'page-size' => 'A4',
  // 'viewport-size' => '1280x1024',
  'header-html' => $header,
  'footer-html' => $footer,
  'margin-left' => '10mm',
  'margin-right' => '10mm',
  'margin-top' => '30mm',
  'margin-bottom' => '25mm',
 ]);*/
    //$snappy->setOption('header-html', $header);
    // $snappy->getOutputFromHtml($html);
    /*$options = [
     'header-html' => $header,
     'footer-html' => $footer
    ];*/

    /*return new PdfResponse(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html, $options),
     'file.pdf'
    );*/

    /*return new Response(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html,$options),
     200,
     [
      'Content-Type'        => 'application/pdf',
      'Content-Disposition' => sprintf('attachment; filename="%s"', "test"),
     ]
    );*/
  }

I don't know what is the problem, thanks in advance!!

0

There are 0 answers