Symfony 2 kpn snappy generate pdf with output meets security area

╄→гoц情女王★ 提交于 2020-01-01 03:36:12

问题


I`m using Symfony2 kpn snappy bundle to generate pdfs. I want to generate PDF from an html page with css. I found a solution, but it has a problem with :

    $pageUrl = $this->generateUrl('accounts_management_generate_pdf_markup',
    array('invoice' => $invoiceData), true); // use absolute path!

    return new \Symfony\Component\HttpFoundation\Response(
        $this->get('knp_snappy.pdf')->getOutput($pageUrl), 200, array(
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'attachment; filename="file.pdf"'
        )
    );

The problem is that the pageUrl accounts_management_generate_pdf_markup is behind a security area and cannot be accessed without authentication. The generated file is just the login page, to which this path accounts_management_generate_pdf_markup redirects if not logged.

My questions are:

Is there any way to pass to snappy authentication credentials?
Is there another way using snappy bundle to generate the pdf using styles(css)


回答1:


You can add the session cookie as an argument to the getOutput function:

        $pageUrl = $this->generateUrl('route', array('id' => $id), true);
        $session = $this->get('session');
        $session->save();
        session_write_close();

        return new Response(
            $this->get('knp_snappy.pdf')->getOutput($pageUrl, array('cookie' => array($session->getName() => $session->getId()))),
            200,
            array(
                'Content-Type'          => 'application/pdf',
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
            )
        );


来源:https://stackoverflow.com/questions/20286012/symfony-2-kpn-snappy-generate-pdf-with-output-meets-security-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!