【问题标题】:Symfony KNP Snappy Bundle: PDF does not load css fileSymfony KNP Snappy Bundle:PDF 不加载 css 文件
【发布时间】:2017-07-02 01:58:07
【问题描述】:

我正在使用 knp-snappy-bundle 从树枝中生成 PDF。

但是我的 css 文件没有加载。

我用static urlabsolute url 和只有asset url 进行了尝试,注意到工作正常,但这些是正常的css files,例如在我的编辑表单中显示时我也会使用它。这是我在pdf.html.twig 文件中尝试的三个选项

  // STATIC DOES NOT WORK
  <link rel="stylesheet" href="https://localhost/test/boot.css">

 // ASSET DOES NOT WORK
    <link rel="stylesheet" href="{{ asset('test/boot.css') }}">


// ABSOLUTE DOES NOT WORK
       <link rel="stylesheet" href="{{ absolute_url(asset('test/boot.css')) }}">

总是得到这个:Warning: Failed to load https://localhost/test/boot.css (ignore)

【问题讨论】:

  • 您是否在与 80/443 不同的端口上运行带有 PHP 内置服务器的 Symfony?
  • 不,我使用的是 80/443
  • 尝试使用绝对网址。示例:{{ absolute_url(asset('css/bootstrap.css')) }}

标签: php css symfony symfony-2.3 symfony-3.1


【解决方案1】:

我假设您使用的是wkhtmltopdf,在这种情况下,您需要使用 css 文件的绝对路径。不知道您的网络文件夹的路径是什么,但类似这样:

<link rel="stylesheet" href="/var/www/html/Symfony/test/boot.css">

我在wkhtmltopdf 上遇到了同样的问题,不得不这样硬编码。

【讨论】:

    【解决方案2】:

    pdf.html.twig 文件不能用另一个文件扩展。试试这个代码,它可以工作

    <link href="{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('test/boot.css') }}" rel="stylesheet"/>
    

    【讨论】:

      【解决方案3】:

      一旦我们进入 Symfony,你就可以像下面这样在 TWIG 中尝试

      {% stylesheets
         'test/boot.css'
         filter='cssrewrite' %}
         <link rel="stylesheet" href="{{ asset_url }}"/>
      {% endstylesheets %}
      

      您的 css 文件应该位于您的 Symfony 应用程序 web/test 目录中。在浏览器中检查可用性

      http://localhost/test/boot.css
      

      应该工作

      【讨论】:

        猜你喜欢
        • 2014-09-29
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-01-17
        相关资源
        最近更新 更多