2

アプリケーションで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"'

        )
    );   
}

どうすれば問題を解決できますか?

4

3 に答える 3

6

これは少し遅れていますが、まったく同じ問題があり、その解決策を見つけました。-method の 2 番目のパラメーターとしてオプションを渡すことができますgetOutput()。これらのオプションの 1 つ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を参照してください。

于 2014-04-05T12:35:02.873 に答える
0

そのバンドルにも同様の問題がありました。私の場合、スクリプトがコマンドラインから実行されていたという問題がありました。そして問題は、実行されたユーザーがソナタ管理者で認証されなかったことです。

そのため、pdf の呼び出しがログインしているユーザーであることを確認し、本番環境と開発環境を切り替えないでください。セッションが失われ、再ログインする必要があります。

したがって、snappy pdf 生成を呼び出すスクリプトが正しく認証され、sonata_admin_role (sonata 管理者バックエンドへのアクセス) を持っているかどうかを確認してください。

それが役立つことを願っています。

于 2013-08-05T08:13:16.297 に答える
0

2021年:私はまだまったく同じ問題を抱えていましたが、Iris Schafferの受け入れられた解決策が少し汚いことがわかりました. だからここに別の方法があります。現在のコントローラーでhtmlを生成するだけです。

->getOutput() を使用する代わりに、->getOutputFromHtml() を使用します。

/**
 * @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"',
        ]
    );
}
于 2021-02-10T15:58:45.320 に答える