用于生成PDF的Symfony2 Knp-snappy不会导入CSS

Oli*_*ier 13 css php pdf symfony twitter-bootstrap

我想从html.twig模板生成一个pdf,但有些不对劲......

事实上,PDF已经创建了良好的内容,但没有布局.似乎CSS文件不是导入的......

我使用twitter的Bootstrap来管理布局.

这是我的控制器的一部分

 $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'
                                                            );
Run Code Online (Sandbox Code Playgroud)

在这里我的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

希望有人可以帮助我..

PS:抱歉打字错误,英语不是我的母语.

spa*_*row 14

这样提供绝对参数会更容易:

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

  • 仅适用于2.5及更高版本 (2认同)

mic*_*jnr 11

必须使用绝对路径链接资产.所以代替:

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

它应该是:

<link rel="stylesheet" type="text/css" href="http://yourdomain.com/bootstrap/css/bootstrap.css">
Run Code Online (Sandbox Code Playgroud)

我自己有这个问题,这对我来说已经解决了.


小智 5

在使用Symfony 2.7时请注意,Twig已删除asset()函数的绝对参数.

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

有关详细信息,请参阅Symfony 2.7中的新功能:新的Asset组件.