还有另一种方法来指示脚本标记中FOSJsRoutingBundle的路由吗?

时间:2016-06-23 10:08:42

标签: symfony bundle

我正在使用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>

但是当使用捆绑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"'
        )
    );
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的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
       }
    }