How to set orientation = landscape using KnpSnappyBundle?

11,820
$pdf = $this->get('knp_snappy.pdf')->getOutputFromHtml($html,
                                   array('orientation'=>'Landscape',
                                         'default-header'=>true));
Share:
11,820
Reveclair
Author by

Reveclair

Updated on July 28, 2022

Comments

  • Reveclair
    Reveclair almost 2 years

    I'm using Snappy Bundle along with Symfony 2.1.

    I have some question I did not find in the documentation of this bundle :

    • How to set the orientation ?
    • Is there a way to display page numbers ?

    Here is my config.yml for the bundle :

    knp_snappy:
        pdf:
            enabled:    true
            binary:     /home/wkhtmltopdf-i386
            options:    []
    

    Here is one of my Controller to generate a pdf :

    public function exampleAction() {
        $html = $this->renderView('MyBundle:Example:test.pdf.twig', $this->param);  
        return new Response($this->get('knp_snappy.pdf')->getOutputFromHtml($html),200, array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="Test.pdf"'));
    
    } 
    

    Thanks a lot for your help!

  • Reveclair
    Reveclair over 11 years
    Thanks for your answer but that does not work as I have the error "Fatal error: Call to undefined method Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator::setOption(‌​)"
  • Reveclair
    Reveclair over 11 years
    I tried your code and also $pdf = ($this->get('knp_snappy.pdf')->getOutputFromHtml($html), 200, array('orientation'=>'Landscape', 'default-header'=>true,'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="Test.pdf"')); But that does not work :(
  • james_t
    james_t over 11 years
    You've put the parameters in the wrong location, it should look like this: return new Response($this->get('knp_snappy.pdf')->getOutputFromHtml($ht‌​ml, array('orientation'=>'Landscape', 'default-header'=>true)), 200, array('Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="Test.pdf"')));
  • user1383418
    user1383418 over 10 years
    You know what would really be nice? A link to some documentation. I don't see any reference to an 'orientation' option in the Snappy README ~ github.com/KnpLabs/KnpSnappyBundle/blob/master/README.markdo‌​wn
  • userfuser
    userfuser about 10 years
    KNP Snappy is actually a wrapper for a WKHTMLTOPDF shell utility. Here is a link to its manual: wkhtmltopdf.org/usage/wkhtmltopdf.txt and you will find "orientation" and all other options in it.
  • famas23
    famas23 over 3 years
    if you have one document with 2 pages: first page is Landscape and the other one is portrait! How I can do it?