简体   繁体   English

使用tcpdf生成pdf后,Symfony2不会重定向到新页面

[英]Symfony2 does not redirect to a new page after pdf generation with tcpdf

I have a controller which should generate pdf file and after that should redirect to another site. 我有一个控制器,应生成pdf文件,然后应重定向到另一个站点。

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;
}
}

But with this code the pdf is generated and browser downloading it but the page is not redirected. 但是使用此代码会生成pdf,然后浏览器将其下载,但页面不会重定向。

That's not possible. 那不可能 You can only try to use javascript to do that. 您只能尝试使用javascript来做到这一点。 Create a iframe load there the pdf download url and redirect the user after calling the iframe with the pdf with javascript. 在pdf下载网址中创建一个iframe加载,并在通过javascript用pdf调用iframe后重定向用户。 (I don't know if it really works) The other question is why do you need to redirect the user? (我不知道它是否真的有效)另一个问题是为什么您需要重定向用户?

That's normal. 那很正常 Your response has been sent when you dowloaded the file. 下载文件时已发送您的回复。 It's too to send a redirect response. 发送重定向响应也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM