【问题标题】:Is there another way to indicate the route of FOSJsRoutingBundle in a script tag?是否有另一种方法可以在脚本标记中指示 FOSJsRoutingBundle 的路由?
【发布时间】:2016-06-23 10:08:41
【问题描述】:

我正在使用 FOSJsRoutingBundle。我在 Twig 模板中链接文件如下:

<script type="text/javascript" src="{{asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script type="text/javascript" src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>

但是在使用 bundle KnpSnappyBundle 生成 pdf 文件时,我需要绝对指出前面的路线。通过使用 Symfony 2.3,我在使用资产的路径中进行了如下更改:

<script type="text/javascript" src="{{app.request.scheme ~ '://' ~ app.request.httpHost ~ asset('bundles/fosjsrouting/js/router.js') }}"></script>

我的问题是另一条路线给我这个捆绑包的错误。有没有其他的说法?我尝试将与上述相同的方法不起作用,并且使用url 而不是path 不起作用。

这是我遇到的错误:

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv3_server_method
[=======> ] 13%
[==========> ] 18%
[==========================> ] 44%
[============================> ] 47%
[==============================> ] 50%
[==============================> ] 50%
[===========================================> ] 73%
[==============================================> ] 78%
[============================================================] 100%
Counting pages (2/6)
[============================================================] Object 1 of 1
Resolving links (4/6)
[============================================================] Object 1 of 1
Loading headers and footers (5/6)
Printing pages (6/6)
[> ] Preparing
[============================================================] Page 1 of 1
Done
Exit with code 1 due to network error: ContentNotFoundError
"stdout: ""
command: /opt/lampp/htdocs/Symfony/app/../vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 --lowquality --page-size 'A4' --viewport-size '‘1024x768’' '/tmp/knp_snappy576ad837d24a82.40556276.html' '/tmp/knp_snappy576ad837d25009.94614572.pdf'.

这是控制器:

public function GenerarPdfFestivosAction()
{

    $this->render(sprintf('BackendBundle:Festivos:calendario.pdf.twig'));
    $em = $this->getDoctrine()->getManager();

    $inicio =$em->getRepository('BackendBundle:Centro')->findInicioCurso();
    $fin =$em->getRepository('BackendBundle:Centro')->findFinCurso();


    $html = $this->renderView('BackendBundle:Festivos:calendario.html.twig', array(
        'inicio' => $inicio,
        'fin' => $fin,
    ));

    return new Response(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
        200,
        array(
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => 'attachment; filename="Calendario.pdf"'
        )
    );
}

感谢您的帮助。

【问题讨论】:

  • 什么错误?为什么需要js路由来生成pdf?
  • 你好@SylvainGuilbert,我需要在另一个中生成一个包含 Twig 模板的路由,以填充我需要的一些数据。获取 html,但使用此捆绑包将其转换为 pdf 也会给我的路线带来错误。在这个捆绑包中,您必须使用绝对路径,但仍然会出现错误,我想这将是因为 ... / routing? Callback = fos.Router.setData 包含路由。我在声明中添加的错误。在我的情况下,我添加了 load 另一个模板的内容,所以:$('container').load(Routing.generate('holidays_per_month', {id:month}));
  • @SylvainGuilbert 你知道有没有办法把path('fos_js_routing_js', {"callback": "fos.Router.setData"}) 放在其他地方?我真的不知道那条路线,但也许有更明确的指示。不知道是不是和我要生成的路线有关('holidays_per_month')。

标签: symfony bundle


【解决方案1】:

您的 pdf 已生成,您可以像这样使用它 (answer from here): 在您的控制器中:

use Symfony\Component\HttpFoundation\BinaryFileResponse;

    function generatePdfAction($param){
       $generator =  $this->get('knp_snappy.pdf');
       $pdf = null;
       $fileName = '/myDir/Calendario.pdf'; // change this
       $inicio =$em->getRepository('BackendBundle:Centro')->findInicioCurso();
       $fin =$em->getRepository('BackendBundle:Centro')->findFinCurso();
       $html = $this->renderView('BackendBundle:Festivos:calendario.html.twig', array(
                'inicio' => $inicio,
                'fin' => $fin,
               ));
       try {
            $pdf = $generator->getOutputFromHtml($html);
        } catch (\RuntimeException $e) {
            $matches = [];
            if (preg_match('/([^\']+)\'.$/', $e->getMessage(), $matches)) {
                $pdf = file_get_contents($matches[1]);
                unlink($matches[1]);
            } else  {
                throw $e;
            }
        }
       if ($pdf){
          file_put_contents($fileName, $pdf);
          return new BinaryFileResponse($fileName);
       }
       else{
       // error message or smthing
       }
    }

【讨论】:

  • 你好@SylvainGuilbert,对不起,我是新手,你会在哪里使用这个代码?如果不使用给我带来问题的行,我会自动下载我拥有的 pdf 和控制器:return new Response( $this-&gt;get('knp_snappy.pdf')-&gt;getOutputFromHtml($html), 200, array( 'Content-Type' =&gt; 'application/pdf', 'Content-Disposition' =&gt; 'attachment; filename="Calendario.pdf"' ) );
  • 你把这个放在负责生成pdf的控制器函数中,
  • 感谢您的帮助@SylvainGuilbert。我一直在测试修改该代码的控制器,但没有让它工作。我在声明中添加了您使用的驱动程序,您能告诉我应该如何更改它吗? (控制器上显示的变量$inicio$fin需要向模板传递数据)。
  • 谢谢@SylvainGuilbert,现在我有以下错误:ContextErrorException: Warning: file_put_contents(/myDir/Calendario.pdf): failed to open stream: No such file or directory in /opt/......./FestivosController.php line 471对应行代码file_put_contents($fileName, $pdf);
  • 当然 $file 只是一个示例,您应该将 pdf 文件放在现有目录中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多