Wkhtmltopdf重定向到symfony2中的登录页面

sou*_*ane 2 php wkhtmltopdf symfony sonata-admin

我正在使用wkhtmltopdf在我的应用程序中生成pdf报告,但是当生成pdf时,我在pdf中获得了登录页面.

这是我的行动:

public function exportPdfAction($id = 0)
{
    $em = $this->container->get('doctrine')->getEntityManager();
    $id = $this->get('request')->get($this->admin->getIdParameter());
    $object = $this->admin->getObject($id);


    if (!$object) {
        throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
    }

    if (false === $this->admin->isGranted('VIEW', $object)) {
        throw new AccessDeniedException();
    }

    $pageUrl = $this->generateUrl('admin_rh_leave_conge_show', array('id'=>$id), true); // use absolute path!

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

        )
    );   
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

小智 6

这有点晚了,但我遇到了完全相同的问题,并找到了解决方案:你可以在getOutput()-method中传入选项作为第二个参数.其中一个选项是cookie:

use Symfony\Component\HttpFoundation\Response;
...

$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',
    )
);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅http://wkhtmltopdf.org/https://github.com/KnpLabs/KnpSnappyBundle/issues/42.