paciks
paciks

Reputation: 3

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.

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.

Upvotes: 0

Views: 2033

Answers (2)

fkrauthan
fkrauthan

Reputation: 498

That's not possible. You can only try to use javascript to do that. Create a iframe load there the pdf download url and redirect the user after calling the iframe with the pdf with javascript. (I don't know if it really works) The other question is why do you need to redirect the user?

Upvotes: 1

greg0ire
greg0ire

Reputation: 23255

That's normal. Your response has been sent when you dowloaded the file. It's too to send a redirect response.

Upvotes: 0

Related Questions