user10800263
user10800263

Reputation:

How can I create a header without margin in pdf created with snappy twig (Symfony 4)?

I create a pdf in Symfony via snappy from a twig template:

So this is my Controller, where I am creating the pdf:

 /**
  * @Route("/pdf", name="pdf")
  */

  public function pdf(Request $request, Pdf $snappy)
  {

    $snappy->setOption("encoding","UTF-8");

    $html = $this->renderView("default/pdf.html.twig",array(
      "title" => "pdf"
    ));

    $filename = "mypdf";
    return new Response(
      $snappy->getOutputFromHtml($html),
      200,
      array(
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="'.$filename.'.pdf"'
      )
    );

  }

And here is the html template, where I create the header pdf.html.twig:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.header {
  overflow: hidden;
  background-color: #000;
  padding: 20px 10px;
}



.header-right {
  float: right;
}


}
</style>
</head>
<body>

<div class="header">
  <a href="#default" class="logo">Logo</a>

</div>

<div style="padding-left:20px">
  <h1>headline</h1>
  <p>Some text</p>
  <p>Some content..</p>
</div>

</body>
</html>

I try that the black header fits into the page without border. But there is still a white border. It does not go to the edge.

Upvotes: 0

Views: 2351

Answers (2)

user10800263
user10800263

Reputation:

Change this line

 $snappy->setOption("encoding","UTF-8");

into

$snappy->setOption("encoding","UTF-8");
$snappy->setOption('margin-left', '0mm');
$snappy->setOption('margin-right', '0mm');
$snappy->setOption('margin-top', '0mm');

Upvotes: 2

geoB
geoB

Reputation: 4704

The issue is that wkhtmltopdf defaults to non-zero left & right margins. If you modify your executable to wkhtmltopdf -R 0 -L 0 ... the black will extend to the width of the page. See this.

Btw, you may want to remove the extra brace } at the line immediately above </style>

Upvotes: 0

Related Questions