You see, I have the following code:
public function imprimirWord(Juego $j){
$phpWord=new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setThemeFontLang(new Language(Language::ES_ES));
$section=$phpWord->addSection();
$text=$section->addText('Juego Nº '.$j->numero.': '.$j->nombre,array('name'=>'Arial','size' => 15,'bold' => true));
$section->addImage($j->ruta(), array('width'=>400, 'height'=>300, 'align'=>'center'));
$text=$section->addText("-Materiales requeridos: ".$j->materiales);
$text=$section->addText("-Agrupación: ".$j->organizacion);
$text=$section->addText("-Cómo se organiza el juego: ".$j->agrupacion->nombre);
$text=$section->addText("-Desarrollo del juego: ".$j->desarrollo);
if($j->observaciones!='*')
$text=$section->addText("-Reglas y observaciones: ".$j->observaciones);
if($j->variantes!='*')
$text=$section->addText("-Variantes del juego: ".$j->variantes);
$contenidos=$j->enlaces;
$valor=count($contenidos);
if($valor){
$texto="";
foreach($contenidos as $contenido){
$texto.=" ".$contenido->contenido->nombre;
$valor--;
if($valor>1)
$texto.=",";
else if($valor==1)
$texto.=" y";
else
$texto.=".";
}
$text=$section->addText("-Contenidos del juego:".$texto);
}
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$deciso='Juego Nº '.$j->numero.' - '.$j->nombre.'.docx';
$objWriter->save($deciso);
return response()->download(public_path($deciso));
}
This allows me to create a Word file, but there is a problem: It also creates a copy within my project.
To fix it, I have this code: unlink($deciso);
, which is responsible for removing the word file.
And here comes my problem, that when I need to use return
to create the fíchero, if I put this code after the creation of the document, it will not be executed.
I need the user to receive their document without needing a return, so that after it is generated I can delete the document with unlink
.
How do I achieve it?
Edit: Something that I have been suggested is to save the file in the / tmp folder, since it will "restart" over time. When I create the file with save, the project is saved in public_html. How do I save it in the other folder?