ggggino/wordbundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ggggino/wordbundle

最新稳定版本:2.0.0

Composer 安装命令:

composer require ggggino/wordbundle

包简介

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

README 文档

README

This bundle is a wrapper of the library PHPOffice/PHPWord.

For the complete reference look at the PHPWord Docs. PHPWord Docs

License

License

Installation

1 Add to composer.json to the require key

    $composer require ggggino/wordbundle

2 Register the bundle in app/AppKernel.php

    $bundles = array(
        // ...
        new GGGGino\WordBundle\GGGGinoWordBundle(),
    );

Get started

  • Create an empty object:
$phpWordObject = $this->get('phpword')->createPHPWordObject();
  • Create a Word and write to a file given the object:
$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:

  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

Create a new doc

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

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:

$phpTemplateObject->setValue('var1', 'testValue');

Complete example

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

Contribute

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

统计信息

  • 总下载量: 6.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固