1. Go to this page and download the library: Download ggggino/wordbundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php
namespace YOURNAME\YOURBUNDLE\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DefaultController extends Controller
{
public function indexAction($name)
{
// ask the service for a Word2007
$phpWordObject = $this->get('phpword')->createPHPWordObject();
// Create a new Page
$section = $phpWordObject->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
);
// create the writer
$writer = $this->get('phpword')->createWriter($phpWordObject, 'Word2007');
// create the response
$response = $this->get('phpword')->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'stream-file.doc'
);
$response->headers->set('Content-Type', 'application/msword');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
}
php
namespace YOURNAME\YOURBUNDLE\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DefaultController extends Controller
{
public function indexAction($name)
{
$fileName = ".../../test.docx";
// ask the service for a Word2007
$phpTemplateObject = $this->get('phpword')->createTemplateObject($fileName);
$phpTemplateObject->setValue('test', 'testValue');
$phpWordObject = $this->get('phpword')->getPhpWordObjFromTemplate($phpTemplateObject);
// create the writer
$writer = $this->get('phpword')->createWriter($phpWordObject, 'Word2007');
// create the response
$response = $this->get('phpword')->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'stream-file.docx'
);
$response->headers->set('Content-Type', 'application/msword');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.