Php Wkhtmltopdf重定向到symfony2中的登录页面
我正在使用wkhtmltopdf在我的应用程序中生成pdf报告,但生成pdf后,我在pdf中获得了登录页面 这是我的行动:Php Wkhtmltopdf重定向到symfony2中的登录页面,php,symfony,wkhtmltopdf,sonata-admin,Php,Symfony,Wkhtmltopdf,Sonata Admin,我正在使用wkhtmltopdf在我的应用程序中生成pdf报告,但生成pdf后,我在pdf中获得了登录页面 这是我的行动: public function exportPdfAction($id = 0) { $em = $this->container->get('doctrine')->getEntityManager(); $id = $this->get('request')->get($this->admin->getIdPar
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"'
)
);
}
如何解决此问题?我对该捆绑包也有类似的问题。在我的例子中,问题是脚本是从命令行运行的。问题是执行的用户没有在sonata admin中进行身份验证 因此,请确保调用pdf的用户已登录,不要在生产环境和开发环境之间切换,否则将丢失会话,您必须重新登录 因此,请检查调用snappy pdf生成的脚本是否经过正确的身份验证,是否具有sonata_admin_角色(访问sonata admin后端)
希望能有所帮助。这有点晚了,但我遇到了完全相同的问题,并找到了解决方案: 您可以在
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',
)
);
有关详细信息,请参见和。2021:我仍然有完全相同的问题,但发现Iris Schaffer的公认解决方案有点脏。这是另一种方法。您可以在您所在的控制器中生成html 我们使用->getOutputFromHtml()代替->getOutput()
如果您想要一些有用的答案,您可能需要添加更多的细节。因此,我将更新我的问题我的问题已更新似乎这可能是捆绑包的问题:那么,我们无法从安全路由生成pdf?
/**
* @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"',
]
);
}