Symfony2 Knp-snappy to generate PDF doesn't import CSS

11,075

Solution 1

The assets must be linked using absolute paths. So instead of:

<link rel="stylesheet" type="text/css" href="{{ asset('bootstrap/css/bootstrap.css') }}">

It should be:

<link rel="stylesheet" type="text/css" href="http://yourdomain.com/bootstrap/css/bootstrap.css">

I had this issue myself and this sorted it out for me.

Solution 2

It would be easier to supply absolute parameter like that:

<link rel="stylesheet" type="text/css" href="{{ asset('bootstrap/css/bootstrap.css', absolute=true) }}">

Solution 3

Take note when using Symfony 2.7, Twig has removed absolute argument for asset() function.

<link rel="stylesheet" type="text/css" href="{{ absolute_url(asset('bootstrap/css/bootstrap.css')) }}">

See New in Symfony 2.7: the new Asset component for more info.

Solution 4

@user1805558's answer worked for me. I was also using Less, and used this, which some may find helpful to see:

{% block stylesheets %}
    {% stylesheets filter='lessphp' combine=true output='css/pdf.css.twig'
        '../app/Resources/assets/css/pdf.less'
    %}
        <link rel="stylesheet" type="text/css" href="{{ asset(asset_url, absolute=true) }}"/>
    {% endstylesheets %}
{% endblock %}
Share:
11,075
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I would like to generate a pdf from an html.twig template but something wrong...

    In fact, the PDF has been create with the good content but there is no layout. It's seems the CSS files are not import...

    I use Bootstrap from twitter to manage the layout.

    Here the part of my controller

     $filename = "CI-TRI-".$Chrono->getChrono();
                $this->get('knp_snappy.pdf')->generateFromHtml(
                                                                $this->renderView('WebStoreMainBundle:Admin:customInvoiceTemplate.html.twig', array('User'=>$User,'Parts'=>$parts, 'device'=>$device, 'rate'=>$rate)),
                                                                __DIR__.'/../../../../web/download/'.$filename.'.pdf'
                                                                );
    

    And here my layout :

    <html>
    <head>
         {% block stylesheets %}
            <link rel="stylesheet" type="text/css" href="{{ asset('bootstrap/css/bootstrap.css') }}">
            <link rel="stylesheet" type="text/css" href="{{ asset('bootstrap/css/customInvoice.css') }}">
            <base href="http://{{app.request.host}}">
            <meta charset="UTF-8" >
        {% endblock %}
    </head>
    <body>
        {% block header %}
            <div class="span2">
                <img src="{{ asset('bootstrap/img/GTO_logo.png') }}">
            </div>
        {% endblock %}
    
        {% block content %}
    
        {% endblock %}
    
    </body>
    

    Wish someone can help me..

    PS: Sorry for the typos, english is not my native language.

  • od3n
    od3n over 9 years
    only applicable to version 2.5 and above
  • Soullivaneuh
    Soullivaneuh over 8 years
    I got a timeout with this. Any idea?
  • mickburkejnr
    mickburkejnr over 8 years
    @Soullivaneuh completely depends on what you've done so far. Best bet is to check to make sure you can open the CSS through the browser properly. If you can, then I'd post it as a question.
  • Interlated
    Interlated over 8 years
    See this answer for a portable solution stackoverflow.com/questions/17049712/…
  • john Smith
    john Smith about 7 years
    for me this throws a twig error (symfony 2.8), i went with this solution: href="{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset_url }}"