Created
March 9, 2020 15:38
-
-
Save gbelot2003/c63bbc15a474e660c1da909f25b8b47c to your computer and use it in GitHub Desktop.
Ejemplo de usuo de PHPWord con y sin plantillas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class WordController extends Controller | |
{ | |
/** | |
* Generamos un archivo de word | |
* | |
*/ | |
public function index() | |
{ | |
$phpWord = new \PhpOffice\PhpWord\PhpWord(); | |
$section = $phpWord->addSection(); | |
$description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | |
$section->addImage("http://itsolutionstuff.com/frontTheme/images/logo.png"); | |
$section->addText($description); | |
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); | |
$objWriter->save(storage_path('helloWorld.docx')); | |
return response()->download(storage_path('helloWorld.docx')); | |
} | |
/** | |
* Generamos un archivo de word | |
* desde una plantilla en el sistema | |
*/ | |
public function template() | |
{ | |
$path = storage_path() . "/app/public/"; | |
//dd($path . 'template.docx'); | |
$template = new \PhpOffice\PhpWord\TemplateProcessor($path . 'template.docx'); | |
$template->setValue('nombre', 'Gerardo Belot'); | |
$template->setValue('email', '[email protected]'); | |
$template->saveAs(storage_path('template1.docx')); | |
return response()->download(storage_path('template1.docx')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment