phpsymfonypdfsymfony-2.8knp-snappy

How to set page sizing of pdf with Knp Snappy bundle


I have a code which use to work to generate PDF with the KNP Snappy bundlde with Symfony2. This is what it looks like:

     $this->container->get('knp_snappy.pdf')->generateFromHtml(
          $this->templating->render(
             $twigInclude,
             array( ,
                 'htmlFiles' => $htmlFiles,
                 'headlines' => $headlines,
             )
         ),
         $folderHistory . '/reports/' . $filename . '.pdf'
     );

My issue is that, I want to set margins and change page size from A4 to A1. I have found so many examples but everytime I try, it seems like the pdf does not generate anymore. How can I solve this issue and make that work.

Thanks in advance for your help.


Solution

  • There are several flags you can set within options:

    $options = [
      'page-size' => 'A1',
      'margin-top' => 10,
      'margin-right' => 10,
      'margin-bottom' => 10,
      'margin-left' => 10,
    ];
    
    $this->container->get('knp_snappy.pdf')->generateFromHtml(
      $this->templating->render(
        $twigInclude,
        [
          'htmlFiles' => $htmlFiles,
          'headlines' => $headlines,
        ]
      ),
      $folderHistory . '/reports/' . $filename . '.pdf',
      $options,
      true // this flag will overwrite existing file
    );