Olivier
Olivier

Reputation: 133

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.

Upvotes: 13

Views: 12045

Answers (6)

Joey
Joey

Reputation: 136

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.

Upvotes: 5

pyjavo
pyjavo

Reputation: 1613

I had the same problem. As commented in this issue your asset URLs need to be absolute.

This can be accomplished in symfony using the asset twig funtion and "package urls":

http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls

For example:

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

And then, in your app/config/config.yml

framework:
# ...
templating:
    assets_base_urls:
        http:
            - "http://yourdomain.com/"

If you are in your local configuration, inside your config_dev.yml you should use a different URL instead, like:

    - "http://localhost/myproject/web/"

Upvotes: 0

timhc22
timhc22

Reputation: 7451

@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 %}

Upvotes: 2

Jeremy Jumeau
Jeremy Jumeau

Reputation: 199

UP for absolute option in asset call :

<img src="{{ asset('/assets/img/AVNZ-Logo-H-SMALL.jpg', absolute=true) }}">

It's related in official bundle repo : https://github.com/KnpLabs/KnpSnappyBundle/issues/78

Upvotes: 0

sparrow
sparrow

Reputation: 146

It would be easier to supply absolute parameter like that:

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

Upvotes: 14

mickburkejnr
mickburkejnr

Reputation: 3690

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.

Upvotes: 12

Related Questions