Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用KnpSnappyBundle PDF和Symfony2加载CSS和图像?_Php_Symfony_Pdf - Fatal编程技术网

Php 如何使用KnpSnappyBundle PDF和Symfony2加载CSS和图像?

Php 如何使用KnpSnappyBundle PDF和Symfony2加载CSS和图像?,php,symfony,pdf,Php,Symfony,Pdf,我在加载CSS和图像资源时遇到一个需要身份验证的错误 错误示例: Warning: Failed to load http://reporter.dev:8888/css/fc84af4_part_1_bootstrap-editable_2.css (ignore) 我正在使用KNP Snappy捆绑包,如下所示: $html = $this->renderView( 'InterlatedReporter:Allocation:casual_calendar_pdf.html.t

我在加载CSS和图像资源时遇到一个需要身份验证的错误

错误示例:

Warning: Failed to load http://reporter.dev:8888/css/fc84af4_part_1_bootstrap-editable_2.css (ignore)
我正在使用KNP Snappy捆绑包,如下所示:

$html = $this->renderView(
  'InterlatedReporter:Allocation:casual_calendar_pdf.html.twig',
  array(
    'casualCalendar' => $casualCalendar,
  )
);

$filename = "export-casual-calendar-" . date("Ymd") . '.pdf';

return new Response(
  $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
  200,
  array(
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'attachment; filename="' . $filename . '"'
  )
);
细枝模板已添加绝对路径,如下所示。URL似乎使用了以下文件://

{{ app.request.getSchemeAndHttpHost() ~ asset('css/sortable-theme-bootstrap.css') }}
我尝试过使用安全性,dev防火墙已就位:

dev:
    pattern:  ^/(_(profiler|wdt)|css|images|js)/
    security: false
这是第一条规则

我们根据SensioFrameworkExtraBundle使用控制器注释

我也试着通过了会议


如何加载资源?

我找到了一个解决方案。该文件是密钥

将基本目录传递给模板:

$html = $this->renderView(
  'InterlatedReporter.html.twig',
  array(
    'casualCalendar' => $casualCalendar,
    'base_dir' => $this->get('kernel')->getRootDir() . '/../web' . $request->getBasePath()
  )
);
并提供资产的完整路径:

<link rel="stylesheet" href="{{ base_dir ~ asset('css/sortable-theme-bootstrap.css') }}">

我也遇到了同样的问题,Interlated说,在
资产('path/to/your/asset/or/image')之前添加一个基本目录。
解决了这个问题

如果您无法访问
$this->get('kernel')
$request
(例如在侦听器中),您可以使用
\uuuu DIR\uu
神奇常量“手动”访问

我的侦听器位于
/myproject/src/MyBundle/listener/MyListener.php
而我的
$basedir=\uuuuuu DIR./../../../web'

因此,在您的小树枝视图中