简体   繁体   English

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

[英]Wkhtmltopdf redirect to login page in symfony2

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.我正在使用 wkhtmltopdf 在我的应用程序中生成一个 pdf 报告,但是当生成 pdf 时,我在 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.这有点晚了,但我遇到了完全相同的问题,并找到了解决方案:您可以在getOutput()方法中将选项作为第二个参数传递。 One of these options is cookie :这些选项之一是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 http://wkhtmltopdf.org/ and https://github.com/KnpLabs/KnpSnappyBundle/issues/42 for details.有关详细信息,请参阅http://wkhtmltopdf.org/https://github.com/KnpLabs/KnpSnappyBundle/issues/42

I had a similar problem with that bundle.我对那个包有类似的问题。 In my case was the issue that the script was running from the command line.在我的情况下是脚本从命令行运行的问题。 And the problem was that there the executed user was not authenticated in the sonata admin.问题是执行的用户没有在奏鸣曲管理员中进行身份验证。

So be sure your calling the pdf is logged in user and don't switch between production and development environment that will lost the session and you have to relogin.因此,请确保您调用的 pdf 是登录用户,并且不要在生产环境和开发环境之间切换,这将丢失会话并且您必须重新登录。

So check if the script with is calling the snappy pdf generation is correctly authenticated and has the sonata_admin_role (access to the sonata admin backend).因此,请检查调用 snappy pdf 生成的脚本是否经过正确验证并具有 sona_admin_role(访问 sonata 管理后端)。

Hope that helps.希望有帮助。

2021: I still had the exact same problem but found the accepted solution of Iris Schaffer a little bit dirty. 2021 年:我仍然遇到完全相同的问题,但发现 Iris Schaffer 的公认解决方案有点脏。 So here is another way.所以这是另一种方式。 You can just generate the html in the controller you're in.您可以在您所在的控制器中生成 html。

Instead of using ->getOutput() we use ->getOutputFromHtml()我们使用 ->getOutputFromHtml() 而不是使用 ->getOutput()

/**
 * @Route("/dossiers/{dossier}/work-order/download", name="download_work_order")
 * @Security("is_granted('DOWNLOAD_WORK_ORDER', dossier)")
 *
 * @param Dossier $dossier
 * @return Response
 */
public function generateWorkOrderPdfAction(Dossier $dossier): Response
{
    /**
     * Since we are a valid logged-in user in this controller we generate everything in advance
     * So wkhtmltopdf does not have login issues
     */
    $html = $this->forward('PlanningBundle\Controller\WorkOrderController::generateWorkOrderHTMLAction', [
        'dossier' => $dossier,
    ])->getContent();

    $options = [
        'footer-html' => $this->renderView('@Dossier/PDF/footer.html.twig', [
            'dossier' => $dossier,
        ]),
    ];

    return new Response(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html, $options),
        200,
        [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'attachment; filename="work-order-' . $dossier->getName() . '.pdf"',
        ]
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM