2013-07-29 62 views
1

我正在使用wkhtmltopdf在我的應用程序中生成pdf報告,但是當生成pdf時,我得到了pdf中的登錄頁面。Wkhtmltopdf重定向到symfony2中的登錄頁面

這是我的操作:

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

     ) 
    ); 
} 

我怎樣才能解決這個問題呢?

+0

如果你想得到一些有用的答案,你可能不得不提供更多的細節。 – cheesemacfly

+0

所以我會更新我的問題 – soufiane

+0

我的問題更新 – soufiane

回答

0

我有一個類似的問題,捆綁。在我的情況下是腳本從命令行運行的問題。問題在於執行的用戶沒有在奏鳴曲管理員身份驗證。

因此,請確保您的PDF文件以用戶身份登錄,並且不會在生產環境和開發環境之間切換,從而導致會話丟失,並且您必須重新登錄。

因此,檢查是否正在調用snappy pdf代碼的腳本被正確驗證並且具有sonata_admin_role(訪問了sonata admin後端)。

希望有所幫助。

5

這有點遲,但我有完全相同的問題,並找到了解決方案: 您可以將選項作爲第二個參數傳入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', 
    ) 
); 

詳見https://code.google.com/p/wkhtmltopdf/issues/detail?id=356https://github.com/KnpLabs/KnpSnappyBundle/issues/42