Codeigniter - Descargar plantilla ya creada con phpword en codeigniter

 
Vista:

Descargar plantilla ya creada con phpword en codeigniter

Publicado por J4ss4n (1 intervención) el 01/04/2019 17:33:03
Saludos, estoy utilizando la librería phpword con codeigniter y quisiera saber como descargar una plantilla en word que ya tengo creada y no que me descargue un archivo nuevo de word. Este es mi código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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");
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Descargar una plantilla Word existente en CodeIgniter

Publicado por Alejandro (44 intervenciones) el 01/09/2023 23:25:42
Para descargar una plantilla en Word que ya tienes creada en lugar de crear un archivo nuevo desde cero, puedes cargar la plantilla existente y luego descargarla. Aquí tienes una versión modificada de tu código que hace eso:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function word()
{
    // Ruta de la plantilla existente en tu servidor
    $templatePath = '/ruta/a/tu/plantilla/existente.docx';
 
    // Verificar si la plantilla existe
    if (!file_exists($templatePath)) {
        show_error('La plantilla no se encuentra en el servidor.');
    }
 
    // Cargar la plantilla existente
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($templatePath);
 
    // Puedes agregar más contenido o realizar modificaciones en la plantilla si es necesario
 
    // Preparar la descarga del archivo
    $file = 'NombreDelArchivoDescargado.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');
 
    // Guardar y descargar la plantilla modificada
    $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $xmlWriter->save("php://output");
}

Asegúrate de reemplazar `'/ruta/a/tu/plantilla/existente.docx'` con la ruta real de tu plantilla Word en el servidor. Este código carga la plantilla existente, realiza las modificaciones necesarias si es que deseas agregar contenido adicional, y luego la descarga con un nombre de archivo diferente.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar