Convert Figma logo to code with AI

PHPOffice logoPHPWord

A pure PHP library for reading and writing word processing documents

7,206
2,686
7,206
1,133

Top Related Projects

A pure PHP library for reading and writing spreadsheet files

4,355

PHP library generating PDF files from UTF-8 encoded HTML

4,143

Official clone of PHP library to generate PDF documents and barcodes

10,446

HTML to PDF converter for PHP

A DOMPDF Wrapper for Laravel

Quick Overview

PHPWord is a library written in pure PHP that provides a set of classes to write and read various document file formats, including Microsoft Office Word (.docx). It allows developers to create, modify, and convert Word documents programmatically, offering a wide range of formatting options and document elements.

Pros

  • Extensive support for various Word document features and formatting options
  • Easy-to-use API with good documentation and examples
  • Ability to read and write multiple file formats (OOXML, RTF, HTML, PDF)
  • Active development and community support

Cons

  • Performance can be slow when dealing with large documents
  • Limited support for some advanced Word features
  • Dependency on other libraries for certain functionalities (e.g., PDF conversion)
  • Potential memory issues with very complex documents

Code Examples

  1. Creating a simple document:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello, World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
  1. Adding a table to the document:
$table = $section->addTable();
$table->addRow();
$table->addCell(2000)->addText('Cell 1');
$table->addCell(2000)->addText('Cell 2');
$table->addRow();
$table->addCell(2000)->addText('Cell 3');
$table->addCell(2000)->addText('Cell 4');
  1. Applying text formatting:
$fontStyle = array('name' => 'Arial', 'size' => 16, 'color' => '1B2232', 'bold' => true);
$paragraphStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100);
$section->addText('Formatted Text', $fontStyle, $paragraphStyle);

Getting Started

  1. Install PHPWord using Composer:
composer require phpoffice/phpword
  1. Create a new PHP file and include the Composer autoloader:
<?php
require 'vendor/autoload.php';

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;

$phpWord = new PhpWord();
// Your code here to create and manipulate the document
$writer = IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('document.docx');

Competitor Comparisons

A pure PHP library for reading and writing spreadsheet files

Pros of PhpSpreadsheet

  • More comprehensive support for Excel features and formulas
  • Better performance when handling large spreadsheets
  • More active development and frequent updates

Cons of PhpSpreadsheet

  • Steeper learning curve due to more complex API
  • Higher memory usage, especially for large files
  • Limited support for older Excel formats (pre-2007)

Code Comparison

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

PhpSpreadsheet:

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('helloWorld.xlsx');

Both libraries offer similar ease of use for basic operations, but PhpSpreadsheet provides more advanced features for spreadsheet manipulation. PHPWord is more focused on document creation and formatting, while PhpSpreadsheet excels in data processing and complex calculations within spreadsheets.

4,355

PHP library generating PDF files from UTF-8 encoded HTML

Pros of mpdf

  • Specialized in PDF generation from HTML and CSS
  • Supports complex layouts and Unicode characters
  • Smaller file size and faster execution for PDF-specific tasks

Cons of mpdf

  • Limited to PDF output format
  • Less flexibility in document structure manipulation
  • Steeper learning curve for non-HTML/CSS users

Code Comparison

mpdf:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World</h1>');
$mpdf->Output('document.pdf', 'F');

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World');
\PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF')->save('document.pdf');

Key Differences

  • mpdf focuses on PDF generation from HTML/CSS, while PHPWord offers multi-format document creation
  • PHPWord provides more programmatic control over document structure
  • mpdf excels in complex layouts and Unicode support for PDF output
  • PHPWord offers easier integration with other document formats (DOCX, ODT, RTF)

Use Cases

  • Choose mpdf for HTML-to-PDF conversion with advanced layout requirements
  • Opt for PHPWord when working with multiple document formats or needing fine-grained control over document structure
4,143

Official clone of PHP library to generate PDF documents and barcodes

Pros of TCPDF

  • Specialized in PDF generation with advanced features like digital signatures and encryption
  • Extensive support for various image formats and barcode types
  • Lightweight and doesn't require external libraries

Cons of TCPDF

  • Limited to PDF creation, unlike PHPWord's support for multiple document formats
  • Steeper learning curve due to its comprehensive feature set
  • Less active development and community support compared to PHPWord

Code Comparison

TCPDF example:

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello, World!', 0, 1);
$pdf->Output('example.pdf', 'F');

PHPWord example:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello, World!');
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('example.docx');

Both libraries offer straightforward ways to create documents, but TCPDF focuses on PDF generation with more advanced features, while PHPWord provides flexibility in working with various document formats, including Word documents.

10,446

HTML to PDF converter for PHP

Pros of dompdf

  • Specializes in converting HTML to PDF, making it ideal for web-based content
  • Supports CSS3 and various image formats, allowing for rich styling options
  • Lightweight and easy to integrate into existing PHP projects

Cons of dompdf

  • Limited to PDF generation, lacking support for other document formats
  • May struggle with complex layouts or large documents
  • Less actively maintained compared to PHPWord

Code Comparison

dompdf:

require_once 'dompdf/autoload.inc.php';
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Hello, World!</h1>');
$dompdf->render();
$dompdf->stream("document.pdf");

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello, World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('document.docx');

Both libraries offer straightforward ways to create documents, but dompdf focuses on HTML-to-PDF conversion, while PHPWord provides more versatile document creation capabilities across multiple formats.

A DOMPDF Wrapper for Laravel

Pros of laravel-dompdf

  • Seamless integration with Laravel framework
  • Lightweight and easy to use for PDF generation
  • Supports HTML to PDF conversion out of the box

Cons of laravel-dompdf

  • Limited functionality for complex document manipulation
  • May struggle with advanced formatting and layout options
  • Less suitable for creating Word documents or other file formats

Code Comparison

laravel-dompdf:

use Barryvdh\DomPDF\Facade\Pdf;

$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

laravel-dompdf is ideal for Laravel projects requiring simple PDF generation from HTML, while PHPWord offers more comprehensive document creation and manipulation capabilities across various formats. The choice depends on specific project requirements and the desired output format.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PHPWord

Latest Stable Version Coverage Status Total Downloads License

Branch Master : 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.