Skip to content

Instantly share code, notes, and snippets.

@gbelot2003
Created March 9, 2020 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbelot2003/c63bbc15a474e660c1da909f25b8b47c to your computer and use it in GitHub Desktop.
Save gbelot2003/c63bbc15a474e660c1da909f25b8b47c to your computer and use it in GitHub Desktop.
Ejemplo de usuo de PHPWord con y sin plantillas
<?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', 'gbelot2003@hotmail.com');
$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