CodeIgniter Forums
Descargar plantilla ya creada en word con phpword - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: Descargar plantilla ya creada en word con phpword (/showthread.php?tid=73217)



Descargar plantilla ya creada en word con phpword - J4ss4n - 04-01-2019

Saludos comunidad, necesito ayuda con la librería phpword.

Necesito poder descargar una plantilla de word ya creada con la librería de phpword. Esta es mi función que esta en el controlador:

public function word(){
$phpWord = new \PhpOffice\PhpWord\PhpWord();
 
$section = $phpWord->addSection();
 
$section->addText('"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)');
 
$section->addText('Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness. (Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10));
 
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle($fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
         
$section->addText('"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
$fontStyleName);
 
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
 
$file = 'HelloWorld.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
}


RE: Descargar plantilla ya creada en word con phpword - Jose Lujan - 07-01-2021

someto a su consideración el siguiente ejemplo:


PHP Code:
<?php
/* @property phpword_model $phpword_model */
include_once(APPPATH."third_party/PhpWord/Autoloader.php");
include_once(
APPPATH."core/Front_end.php");

use 
PhpOffice\PhpWord\Autoloader;
use 
PhpOffice\PhpWord\Settings;

Autoloader::register();
Settings::loadConfig();


class 
Phpword extends Front_end {

function 
__construct(){
  parent::__construct();
$this->load->model('phpword_model');
  }

public function 
download()  {
$this->load->library('Phpword');

//$news = $this->phpword_model->get_news();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('application/third_party/PhpWord/resources/informe.docx');

$year date('Y'strtotime('now'));
$month date('m'strtotime('now'));
$monthName date('F'mktime(000$month10));
$day date('d'strtotime('now'));
$filename 'INF Nº NUMBER-'.$year.'InformeCodeigniter'$monthName.'.docx';
$templateProcessor->setValue('year'$year);
$templateProcessor->setValue('month'$monthName);
$templateProcessor->setValue('day'$day);

$templateProcessor->saveAs($filename);
// send results to browser to download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' filesize($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;



}



NOTA: funciona para versiones menores a CodeIgniter-3.1.6
NOTA 2: si alguien sabe como aplicarlo a versiones superiores a 3.1.5 favor de compartir sintaxis.