Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 542
Olivier Avatar asked Aug 06 '14 07:08

Olivier


1 Answers

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.

like image 120
mickburkejnr Avatar answered Sep 19 '22 00:09

mickburkejnr