簡體   English   中英

Wkhtmltopdf重定向到symfony2中的登錄頁面

[英]Wkhtmltopdf redirect to login page in symfony2

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

        )
    );   
}

我該如何解決這個問題?

這有點晚了,但我遇到了完全相同的問題,並找到了解決方案:您可以在getOutput()方法中將選項作為第二個參數傳遞。 這些選項之一是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',
    )
);

有關詳細信息,請參閱http://wkhtmltopdf.org/https://github.com/KnpLabs/KnpSnappyBundle/issues/42

我對那個包有類似的問題。 在我的情況下是腳本從命令行運行的問題。 問題是執行的用戶沒有在奏鳴曲管理員中進行身份驗證。

因此,請確保您調用的 pdf 是登錄用戶,並且不要在生產環境和開發環境之間切換,這將丟失會話並且您必須重新登錄。

因此,請檢查調用 snappy pdf 生成的腳本是否經過正確驗證並具有 sona_admin_role(訪問 sonata 管理后端)。

希望有幫助。

2021 年:我仍然遇到完全相同的問題,但發現 Iris Schaffer 的公認解決方案有點臟。 所以這是另一種方式。 您可以在您所在的控制器中生成 html。

我們使用 ->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