2017 © Pedro Peláez
 

symfony-bundle wordbundle

This is a Symfony2 Bundle helps you to read and write Word files, thanks to the PHPWord library

image

ggggino/wordbundle

This is a Symfony2 Bundle helps you to read and write Word files, thanks to the PHPWord library

  • Monday, November 6, 2017
  • by ggggino
  • Repository
  • 0 Watchers
  • 0 Stars
  • 590 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 163 % Grown

The README.md

Symfony2 Word bundle

This bundle is a wrapper of the library PHPOffice/PHPWord., (*1)

For the complete reference look at the PHPWord Docs. PHPWord Docs, (*2)

License

License, (*3)

Installation

1 Add to composer.json to the require key, (*4)

``` shell $composer require ggggino/wordbundle, (*5)


**2** Register the bundle in ``app/AppKernel.php`` ``` php $bundles = array( // ... new GGGGino\WordBundle\GGGGinoWordBundle(), );

Get started

  • Create an empty object:

``` php $phpWordObject = $this->get('phpword')->createPHPWordObject();, (*6)


- Create a Word and write to a file given the object: ```php $writer = $this->get('phpword')->createWriter($phpWordObject, 'Word2007'); $writer->save('file.xls');
  • Create a Word and create a StreamedResponse:
$writer = $this->get('phpword')->createWriter($phpWordObject, 'Word2007');
$response = $this->get('phpword')->createStreamedResponse($writer);

Not Only 'Word2007'

The list of the types are:, (*7)

  1. 'Word2007'
  2. 'ODText'
  3. 'HTML'
  4. 'PDF'
  5. 'RTF'

Example

You could find a lot of examples in the official PHPWord repository https://github.com/PHPOffice/PHPWord/tree/develop/samples, (*8)

Create a new doc

``` php namespace YOURNAME\YOURBUNDLE\Controller;, (*9)

use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\ResponseHeaderBag;, (*10)

class DefaultController extends Controller {, (*11)

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;        
}

}, (*12)


### Edit a doc In the template file(docx) variable should be declared as ${var1}, so in the template you can change "var1" value in this way: ```php $phpTemplateObject->setValue('var1', 'testValue');

Complete example, (*13)

``` php namespace YOURNAME\YOURBUNDLE\Controller;, (*14)

use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\ResponseHeaderBag;, (*15)

class DefaultController extends Controller {, (*16)

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;        
}

} ```, (*17)

Contribute

  1. fork the project
  2. clone the repo
  3. submit a PullRequest

The Versions

06/11 2017

dev-master

9999999-dev https://github.com/GGGGino

This is a Symfony2 Bundle helps you to read and write Word files, thanks to the PHPWord library

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Ginanni

symfony2 bundle word office

06/11 2017
06/07 2017
06/07 2017
04/07 2017
04/07 2017

v1.0.2

1.0.2.0 https://github.com/GGGGino

This is a Symfony2 Bundle helps you to read and write Word files, thanks to the PHPWord library

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Ginanni

symfony2 bundle word office

04/07 2017
04/07 2017