13

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.

Olivier
  • 133
  • 1
  • 1
  • 4

6 Answers6

14

It would be easier to supply absolute parameter like that:

<link rel="stylesheet" type="text/css" href="{{ asset('bootstrap/css/bootstrap.css', absolute=true) }}">
sparrow
  • 147
  • 1
  • 5
12

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.

mickburkejnr
  • 3,652
  • 12
  • 76
  • 109
  • I got a timeout with this. Any idea? – Soullivaneuh Aug 14 '15 at 15:43
  • @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. – mickburkejnr Aug 18 '15 at 08:07
  • 1
    See this answer for a portable solution http://stackoverflow.com/questions/17049712/how-to-use-absolute-path-in-twig-functions – Interlated Sep 10 '15 at 04:09
5

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.

Joey
  • 126
  • 1
  • 4
2

@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 %}
timhc22
  • 7,213
  • 8
  • 48
  • 66
  • for me this throws a twig error (symfony 2.8), i went with this solution: `href="{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset_url }}"` – john Smith May 05 '17 at 10:11
0

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

Jeremy Jumeau
  • 199
  • 2
  • 4
0

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/"
pyjavo
  • 1,598
  • 2
  • 23
  • 41