Giter Site home page Giter Site logo

phpword's Introduction

PHPWord - OpenXML - Read, Write and Create Word documents in PHP

PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software.

Want to contribute?

Fork us!

Requirements

  • PHP version 5.3.0 or higher

License

PHPWord is licensed under LGPL (GNU LESSER GENERAL PUBLIC LICENSE)

Installation

It is recommended that you install the PHPWord library through composer. To do so, add the following lines to your composer.json.

{
    "require": {
       "phpoffice/phpword": "dev-master"
    }
}

Usage

The following is a basic example of the PHPWord library.

$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Hello world!');

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');

// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Images

You can add images easily using the following example.

$section = $PHPWord->createSection();
$section->addImage('mars.jpg');

Images settings include:

  • width width in pixels
  • height height in pixels
  • align image alignment, left, right or center
  • marginTop top margin in inches, can be negative
  • marginLeft left margin in inches, can be negative
  • wrappingStyle can be inline, square, tight, behind, infront

To add an image with settings, consider the following example.

$section->addImage(
   'mars.jpg',
   array(
       'width' => 100,
       'height' => 100,
       'marginTop' => -1,
       'marginLeft' => -1,
       wrappingStyle => 'behind'
   )
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.