我有一个控制器,它应该生成 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;
}
}
但是使用此代码会生成 pdf 并且浏览器会下载它,但不会重定向页面。