Giter Site home page Giter Site logo

phpoffice / phpword Goto Github PK

View Code? Open in Web Editor NEW
7.1K 312.0 2.7K 38.54 MB

A pure PHP library for reading and writing word processing documents

Home Page: https://phpoffice.github.io/PHPWord/

License: Other

PHP 99.90% XSLT 0.04% HTML 0.02% Rich Text Format 0.04%
php libreoffice-writer msword doc docx html odt pdf rtf office

phpword's Introduction

PHPWord

Latest Stable Version Coverage Status Total Downloads License CI Join the chat at https://gitter.im/PHPOffice/PHPWord

PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), Rich Text Format (RTF), HTML, and PDF.

PHPWord is an open source project licensed under the terms of LGPL version 3. PHPWord is aimed to be a high quality software product by incorporating continuous integration and unit testing. You can learn more about PHPWord by reading the Developers' Documentation.

If you have any questions, please ask on StackOverFlow

Read more about PHPWord:

Features

With PHPWord, you can create OOXML, ODF, or RTF documents dynamically using your PHP scripts. Below are some of the things that you can do with PHPWord library:

  • Set document properties, e.g. title, subject, and creator.
  • Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
  • Create header and footer for each sections
  • Set default font type, font size, and paragraph style
  • Use UTF-8 and East Asia fonts/characters
  • Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
  • Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
  • Insert titles (headers) and table of contents
  • Insert text breaks and page breaks
  • Insert and format images, either local, remote, or as page watermarks
  • Insert binary OLE Objects such as Excel or Visio
  • Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
  • Insert list items as bulleted, numbered, or multilevel
  • Insert hyperlinks
  • Insert footnotes and endnotes
  • Insert drawing shapes (arc, curve, line, polyline, rect, oval)
  • Insert charts (pie, doughnut, bar, line, area, scatter, radar)
  • Insert form fields (textinput, checkbox, and dropdown)
  • Create document from templates
  • Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
  • ... and many more features on progress

Requirements

PHPWord requires the following:

Installation

PHPWord is installed via Composer. To add a dependency to PHPWord in your project, either

Run the following to use the latest stable version

composer require phpoffice/phpword

or if you want the latest unreleased version

composer require phpoffice/phpword:dev-master

Getting started

The following is a basic usage example of the PHPWord library.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->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)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

More examples are provided in the samples folder. For an easy access to those samples launch php -S localhost:8000 in the samples directory then browse to http://localhost:8000 to view the samples. You can also read the Developers' Documentation for more detail.

Contributing

We welcome everyone to contribute to PHPWord. Below are some of the things that you can do to contribute.

phpword's People

Contributors

0b10011 avatar ahrens-sven avatar carusogabriel avatar csk83 avatar dependabot[bot] avatar gabrielbull avatar hazington avatar hskrtich avatar ivanlanin avatar jeroenmoors avatar jhfangying avatar jonnsn avatar kernusr avatar liborm85 avatar lubosdz avatar manunchik avatar mariahaubner avatar mussbach avatar nicoder avatar oleibman avatar powerkiki avatar progi1984 avatar sailormax avatar sergeizelenyi avatar shaedrich avatar simivar avatar spikex avatar terryzwt avatar trainmaster avatar troosan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpword's Issues

row span?

how do I create cells with rowspan more than 1?

Create develop branch

Unstable version should not be the master branch but another branch named develop.

Postprocessing of documents returned by Template class before moving to final destination

Currently, Template class has the "save()" method with the followed signature.

/**
 * Save Template
 *
 * @param string $strFilename
 */
public function save($strFilename)
{
    ...
}

In most cases that's fine, but in some situations this approach it's not optimal enough.

For example, we have a batch of templates and we need to perform the followed actions with them.
a) Substitute macroses in each template.
b) Pack all the result documents in ZIP-archive and move this archive somewhere.

In such situtation Template class of current implementation allows us to do this, but it will require to save processed templates somewhere which involves unnecessary file copying. Moreover, we will have to delete these garbage files after archiving.

So, suggestion is to give options.

  1. To save the processed template in the current location (temporary directory) with it's current unique name. This allows user to do whatever he wants with the document in temporary directory before the file will be moved to final destination.
/**
 * Save Template
 *
 * @return string
 */
public function save() {
    ...
}
  1. To save the processed template in the user defined location with user defined filename.
/**
 * Save Template As...
 *
 * @param string $strFilename
 */
public function saveAs($strFilename) {
    ...
}

UTF8 encoding

I cannot use UTF8 signs, like " ' " or โ‚ฌ, but get a ? instead.

Implement HTML reader

On the odd occasion you want to turn some HTML into a PHPWord object, it would be nice to have a utility for doing so.

Namespacing

Should the namespacing be done like this:

namespace PHPWord;

class PHPWord
{}

which will result in using the lib like this: new PHPWord\PHPWord();

OR

namespace PHPOffice\PHPWord;

class PHPWord
{}

which will result in using the lib like this: new PHPOffice\PHPWord\PHPWord();

Both of which are PSR-0 and PSR-4 compliant.

Word2007 writer does not reset PHPWord_Media::_sectionMedia

I'm using the GIT branch "custom-numbered-lists" of your PHPWord code.
I'm generating several docx documents in a row inside my php module. The documents contain images. I realised that when generating the second document the PHPWord_Writer_Word2007 tried to access the images of the first document. I think this is due to the fact that the static information in PHPWord_Media is not reset.
I wrote a simple reset function and now everything works as expected.

public static function resetMedia() {
    self::$_sectionMedia = array(
            'images' => array(),
            'embeddings' => array(),
            'links' => array());
    self::$_headerMedia = array();
    self::$_footerMedia = array();
}

I suppose such a method should actually be called when calling
PHPWord_IOFactory::createWriter(..., 'Word2007');

I don't know if this also affects other branches of the code.

Suport on table rowspan e colspan

First I want to say congratulations on the project too. Now one thing that was sorely lacking in the styles used in the table was colspan and rowspan properties because they are widely used in creating tables. Now if you go as a way to debug irrei also assist in this project because it is of much interest

Ability to apply XSL style sheet to Template

Sometimes we need to perform more complex actions with document template than just simple replace of ${} parameters. XSLT really helps in such situations.

This issue is a proposal of the correspondent class method implementation with the followed signature.

/**
 * Applies XSL style sheet to XML template.
 *
 * @param DOMDocument &$xslDOMDocument
 * @param array $xslOptions = array()
 * @param string $xslOptionsURI = ''
 */
public function applyXslStyleSheet(&$xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '')
{
    ...
}

Assumption: we have XSL style sheet loaded as DOMDocument before the method call.
Requirement: "php_xml.dll" library.
Limitation: XSLT 1.0, XPath 1.0 (because of XSLTProcessor).

addTextBreak styling?

textBreak's don't seem to follow any sort of style. I could be wrong. I'd like to think it'd follow a paragraphStyle or the last textStyle used. textBreaks for my document seem to large.

Anywhere position image

We need new feature for text/image wrapping. Today is very difficult align images.

maybe I can fork this project and contribute to it.

someone already did it?

Change setIndent and setIndentString to be more compatible

The files PHPWord create seem to be malformed for OpenOffice because PHPWord formats the xml with spaces and newlines. Word removes this lines and shows the document how its supposed to be, but OO inserts a newline for every newline in the xml-doc in the document is shows. Changing these line in Shared/XMLWriter.php fix this.

$this->_xmlWriter->setIndent(false);
$this->_xmlWriter->setIndentString('')

There is a comment in the code saying this might need to be turned off for production.

Restart ListItem Numbering

Is there a way to restart the ListItem numbering (lettering)?

Using this code snippet:

$numberStyleList = array('listType' => \PHPWord_Style_ListItem::TYPE_NUMBER);
$alphStyleList = array('listType' => \PHPWord_Style_ListItem::TYPE_ALPHANUM);

$section->addListItem('One', 0, null, $numberStyleList);
$section->addListItem('Two', 0, null, $numberStyleList);
$section->addListItem('Alpha', 1, null, $alphStyleList);
$section->addListItem('Beta', 1, null, $alphStyleList);
$section->addListItem('Three', 0, null, $numberStyleList);
$section->addListItem('Charlie', 1, null, $alphStyleList);

Currently it would output this:

1. One
2. Two
   A. Alpha
   B. Beta
3. Three
   C. Charlie

I want to output this:

1. One
2. Two
   A. Alpha
   B. Beta
3. Three
   A. Charlie

Is there some other code call that needs to be made? Or is this a limitation of PHPWord?

Implementing textRuns in RTF & ODT Writer

$this->_writeTextRun($objWriter, $element);
is not yet implemented, in both (basic) writers.

Unfortunately the HTML to docx Converter (http://htmltodocx.codeplex.com/) makes heavy use of textruns, so if you want to export userinput, made by a WYSIWYG Editor, to .rtf or .odt, there is currently no possibility for that.
So implementing at least this function would make the 2 writers become more usefull...

thx!
sahib

Add Github Pages

Duplicate PHPOffice/PHPExcel pages and adapt them for PHPWord

Temporary files naming logic in PHPWord_Template can lead to a collision

Temporary files naming logic in PHPWord_Template can lead to a collision

The problem is in the followed peace of code:

public function __construct($strFilename) {
    $path = dirname($strFilename);
    $this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';

    copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File

    $this->_objZip = new ZipArchive();
    $this->_objZip->open($this->_tempFileName);

    $this->_documentXML = $this->_objZip->getFromName('word/document.xml');
}

As we may see, temporary files are named using "the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". In this case, if we have several requests per one second, the code will generate us files with exactly the same names, and that may lead to some confusions. So, we need to generate really unique filenames. tempnam() function meets this requirement.

Moreover, according to ETL, it's not correct to transform data right in the source we extract this data from. But this is what the peace of code does while creating temporary file in the datasource directory. It's not correct to generate temporary files in the destination directory too. There may be tons of such destination directories and, in case of bugs in our code, they may be populated with garbage temporary files. So, we need separate temporary directory for our temporary files, which allows us easily maintain it's content (when deal with garbage). sys_get_temp_dir() function meets this requirement.

Finally, what we need to do is to correct couple lines of code. :)

Implement Daisy XML Document (DTBook)

Link :

Format :

Validator :

Samples :

ToDo :

  • Implement basic features (text, tables, images)
  • Validate locally
  • Unit Tests
  • Validate files with Travis-CI
  • Documentation (Differences with others format, Specifications for the Daisy Format)

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Mission to 1.0.0

WPS Writer (#69), Daisy XML Writer (#54), and ePub2 Writer (#55) will be implemented later.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Alignment footer PreserveText

Alignment of the PreserveText can only be done by entering an emty fontstyle array.
Leaving out the fontsyle array will result in left alignment of the text.

Adding a value that contains "&" in a template breaks it

I'm using the template stuff in order to replace some values, but the following code breaks the template (anything in the template after the & symbol is not shown):

        $document = $PHPWord->loadTemplate($templatePath);
        $document->setValue('type', "some value & foobar");

I guess the & is a specific character in xml word files, like it can be in html

The workaround I'm currently using is to wrap the value into an htmlspecialchars and all is ok afterwards:

        $document = $PHPWord->loadTemplate($templatePath);
        $document->setValue('type', htmlspecialchars("some value & foobar"));

=> why not add this htmlspecialchars right into the setValue function ?

Thanks for your feedback

Cannot use variables in header / footer for template

I'm using the loadTemplate() function to set values in a template.
It works great except for values that are in header / footer. They're not replaced

The reason is because PHPWord only replaces values in the body, and not in the header / footer.

It has been quoted here, with a patch to make it work:
http://phpword.codeplex.com/discussions/236472

with a more generic way here: https://phpword.codeplex.com/workitem/69

Would be nice to have this feature in the official PHPWord :)

Example in README.md is broken

The readme file refers this example:
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

However these functions are (no longer?) implemented. I'm not sure if they should be implemented or if the example should be changed.

Ability to limit number of replacements performed by setValue() method of Template class

Sometimes we need to replace only several string occurrences in text. Current implementation of the corresponded method of Template class looks like as followed

/**
 * Set a Template value
 *
 * @param mixed $search
 * @param mixed $replace
 */
public function setValue($search, $replace)
{
    ...

    $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}

This gives us no chance to replace, for example, the first occcurrence only.

This issue is a proposal of setValue method inhancement by implementing the followed signature:

/**
 * Set a Template value
 *
 * @param mixed $search
 * @param mixed $replace
 * @param integer $limit
 */
public function setValue($search, $replace, $limit = -1) {
    ...
}

rights after creating Sample_02_TabStops.php

After doing Sample_02_TabStops.php

i got:
luuk@opensuse:~/public_html/PHPWord-master/samples> ll Sample_02*
-rw-r--r-- 1 luuk users 8309 jan 4 18:31 Sample_02_TabStops.docx
-rw-r--r-- 1 luuk users 3052 jan 4 18:31 Sample_02_TabStops.odt
-rwxrwxr-x 1 wwwrun users 1807 dec 17 11:45 Sample_02_TabStops.php
-rw-rw-r-- 1 wwwrun users 307 jan 4 18:31 Sample_02_TabStops.rtf

Is it not odd that the docx and odt file are created with user-rights, and that the rtf includes the group rights?

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.