2013-01-03 34 views
0

我有一個控制器,它應該生成PDF文件,之後應該重定向到另一個站點。Symfony2的不重定向到一個新的頁面PDF生成後TCPDF

class DocuController extends Controller 
{ 
    public function indexAction() { 
      // some other code 
      if ($request->get('_add_document')) 
      { 
       $form->bindRequest($request); 
       $em = $this->getDoctrine()->getEntityManager(); 
       $em->persist($document);  
       $em->flush(); 
       if ($session->get('journey_id')!=false) 
       { 
        $relation = new DocumentJourneyInJourney(); 
        $relation->setJourney($session->get('journey_id')); 
        $relation->setDocument($document->getId()); 
        $em->persist($relation); 
        $em->flush(); 
       } 
       $pdf=$this->generatePDF($form); 
       $pdf->Output('file.pdf', 'I'); 

       return $this->redirect($this->generateUrl('another_site')); 
      } 
    }   
    return $this->render('Bundle:Page:document.html.twig'); 
} 

public function generatePDF($form) 
{ 
    $pdf = $this->get("wrep.tcpdf")->create(); 
    $pdf->SetTitle('Tittle');   
    $pdf->SetHeaderData('', 0, ' ', ' ', array(0,0,0), array(0,0,0)); 
    $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
    $pdf->SetPrintFooter(false); 
    $pdf->SetAutoPageBreak(TRUE);   
    $pdf->AddPage(); 
    $html = $this->renderView('Bundle:document.twig', 
        array(
        'traveller' => $form["name"]->getData().' '.$form["surname"]->getData(), 
        'document' => $form["full_name"]->getData() 
        ) 
       ); 
    $pdf->writeHTML($html, $ln=false, $fill=false, $reseth=false, $cell=false, $align=''); 
    return $pdf; 
} 
} 

但有了這個代碼生成PDF格式和瀏覽器下載,但該頁面不重定向。

回答

1

這是不可能的。你只能嘗試使用JavaScript來做到這一點。創建一個iframe加載那裏的pdf下載url並在用JavaScript調用帶有pdf的iframe之後重定向用戶。 (我不知道它是否真的有效)另一個問題是爲什麼你需要重定向用戶?

+0

在這個控制器的網站是形式和按鈕'生成PDF',所以我想生成PDF文件,讓用戶下載該文件,然後將他重定向到例如主頁。 – paciks

+0

將用戶重定向到主頁。在主頁添加JavaScript將用戶重定向到pdf。在用戶仍然在主頁上時,PDF下載開始。 – fkrauthan

0

這是正常的。您在下載文件時發送了您的回覆。這也是發送重定向響應。

+0

感謝您的回覆,您能告訴我如何解決我的問題嗎?我的意思是,你認爲在pdf下載後如何完成重定向? – paciks

+0

我沒有任何建議,因爲你不能從pdf重定向,你能嗎?爲什麼不留在你所在的頁面上? – greg0ire

+0

我認爲會有一些解決方案,但我看到沒有其他選擇。感謝幫助。 – paciks

相關問題