PHPWord to PDF

354 views
Skip to first unread message

Fernando Del Fresno

unread,
Nov 27, 2015, 5:08:17 AM11/27/15
to symfony-es
Hola a todos! Quería haceros una consulta a ver si alguien me puede ayudar.
He utilizado PHPWord para generar un documento word mediante una pantilla. Se generaba la plantilla word autocompletada con valores que le asignaba al documento.
Hasta ahí todo muy bien (Copio abajo un ejemplo por si alguien lo usa). 
La cuestión es que ese documento me sale la necesidad de convertirlo a pdf (no vale con guardar en pdf :). Lo que quiero es dar la opción de generar el documento basado en plantilla en Word o PDF. Estoy utilizando las clases PHPWord http://phpword.codeplex.com/ pero no encuentro la forma de poder convertirlo a PDF. 
¿Alguna idea?

Gracias

Ejemplo de uso de PHPWord con pantilla:

         require_once $this->get('kernel')->getRootDir().'/../vendor/PHPWord/Library/PHPWord.php';
        $PHPWord = new PHPWord();
        $document = $PHPWord->loadTemplate($this->get('kernel')->getRootDir().'/../vendor/PHPWord/Library/'.'resources/template.docx');
        
        // Variables on different parts of document
        //$document->setValue('nombre', htmlspecialchars(mb_strtoupper($grupoAlumno->getAlumno()->getNombre(), 'UTF-8'))); // On section/content
        $document->setValue('nombre', htmlspecialchars(mb_strtoupper($grupoAlumno->getAlumno()->getNombre(), 'UTF-8'))); // On section/content
        $document->setValue('apellidos', htmlspecialchars(mb_strtoupper($grupoAlumno->getAlumno()->getApellidos(), 'UTF-8'))); // On section/content
        $document->setValue('centro', htmlspecialchars($escuela->getNombre())); // On section/content
        $document->setValue('idioma', htmlspecialchars(mb_strtoupper($grupoAlumno->getGrupo()->getIdioma(), 'UTF-8'))); // On section/content
        $document->setValue('nivel', htmlspecialchars(mb_strtoupper($grupoAlumno->getGrupo()->getNivel(), 'UTF-8'))); // On section/content
        $document->setValue('diaclase', htmlspecialchars(mb_strtoupper($grupoAlumno->getGrupo()->getDias()->getDescripcion(), 'UTF-8'))); // On section/content
        $document->setValue('hora', htmlspecialchars(mb_strtoupper($grupoAlumno->getGrupo()->getHoras(), 'UTF-8'))); // On section/content
        $document->setValue('fecha_alta', htmlspecialchars($grupoAlumno->getFechaAlta()->format("d/m/Y"))); // On section/content
        $document->setValue('fecha_baja', htmlspecialchars($grupoAlumno->getFechaBajaEfectiva()->format("d/m/Y"))); // On section/content
        $document->setValue('cuota', htmlspecialchars($grupoAlumno->getAlumno()->getCuota()->getCantidad())); // On section/content
        $document->setValue('dia', htmlspecialchars(date("d"))); // On section/content
        $document->setValue('mes', htmlspecialchars($helper->meses[date("n")])); // On section/content
        $document->setValue('ano', htmlspecialchars(date('Y'))); // On section/content

        // At least write the document to webspace:
        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $filename = 'CertificadoMatriculacion.docx';
        //La carpeta debe tener permisos de escritura. Si no falla
        $document->save($filename);
       
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        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
Reply all
Reply to author
Forward
0 new messages