13

Wkhtmltopdf redirect to the login page in symfony2

 3 years ago
source link: https://www.codesd.com/item/wkhtmltopdf-redirect-to-the-login-page-in-symfony2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Wkhtmltopdf redirect to the login page in symfony2

advertisements

I'm using wkhtmltopdf to generate a pdf report in my application,but when the pdf is generated,i got the login page in the pdf.

this is my Action:

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"'

        )
    );
}

how can i resolve the problem ?


this is a bit late, but I had the exact same problem, and found a solution for it: You can pass in options as second parameter in the getOutput()-method. One of these options is 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',
    )
);

See https://code.google.com/p/wkhtmltopdf/issues/detail?id=356 and https://github.com/KnpLabs/KnpSnappyBundle/issues/42 for details.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK