PHP Logophpoffice/phpword

phpoffice/phpword is a powerful PHP library that allows developers to create, read, and edit Microsoft Word documents (.docx, .rtf, .odt) dynamically. It is part of the PhpOffice project, which also includes PhpSpreadsheet for Excel and PhpPresentation for PowerPoint. PhpWord is designed to be easy to use, providing a clean API for common Word document operations without requiring Microsoft Office to be installed on the server.

Purpose:
The primary purpose of phpword is to automate the generation of Word documents from web applications or scripts. This is incredibly useful for tasks such as:
* Generating reports, invoices, or contracts.
* Creating personalized letters or certificates.
* Exporting data from a database into a structured document.
* Automating the creation of complex documents with dynamic content.

Key Features:
* Document Creation: Start from scratch or load existing templates.
* Text Formatting: Apply various styles like bold, italic, underline, font sizes, colors, and more.
* Paragraph Formatting: Control alignment, indentation, line spacing, and paragraph breaks.
* Sections: Define multiple sections within a document, each with its own headers, footers, page numbering, and page orientation.
* Tables: Create complex tables with merged cells, borders, and cell styling.
* Lists: Generate ordered (numbered) and unordered (bulleted) lists.
* Images: Insert images into documents, with options for sizing and positioning.
* Headers & Footers: Add custom headers and footers, including page numbers.
* Page Breaks: Insert manual page breaks.
* Hyperlinks: Embed clickable links.
* Watermarks: Add text or image watermarks.
* Comments: Add comments to specific text.
* Drawing Objects: Basic support for shapes.
* Templating: Use existing .docx files as templates, replacing placeholders with dynamic data.
* Output Formats: Save documents in .docx (Word 2007+), .rtf, and .odt formats.

Installation:
PhpWord is typically installed via Composer, the PHP dependency manager. You would run `composer require phpoffice/phpword` in your project directory.

Basic Usage:
The general workflow involves creating a new `PhpWord` object, adding sections to the document, adding content (text, tables, images) to those sections, and then using a `IOFactory` writer to save the document to a file or stream it to the browser.

Example Code

<?php
require_once 'vendor/autoload.php';

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

// Create a new PhpWord object
$phpWord = new PhpWord();

// Add a section to the document
$section = $phpWord->addSection();

// Add text to the section
$section->addText(
    'This is an example document created using PhpOffice\\PhpWord library.',
    ['name' => 'Times New Roman', 'size' => 14, 'bold' => true]
);

$section->addText(
    'PhpOffice\\PhpWord is a PHP library written in pure PHP that provides a set of classes for writing and reading different document file formats.',
    ['name' => 'Arial', 'size' => 12]
);

// Add a paragraph break with some spacing
$section->addTextBreak(2);

// Add more text with different formatting
$section->addText(
    'It supports creating complex documents including tables, lists, images, and headers/footers.',
    ['name' => 'Verdana', 'size' => 10, 'italic' => true, 'color' => '0000FF']
);

// Add a page break
$section->addPageBreak();

// Add another section (or continue in the same section after page break)
$section->addText(
    'This text appears on the next page after a manual page break.',
    ['name' => 'Courier New', 'size' => 12]
);

// Add a simple table
$section->addTextBreak(1);
$section->addText('Here is a simple table:');

$table = $section->addTable();
$table->addRow();
$table->addCell(1750)->addText('Row 1, Cell 1');
$table->addCell(1750)->addText('Row 1, Cell 2');
$table->addCell(1750)->addText('Row 1, Cell 3');

$table->addRow();
$table->addCell(1750)->addText('Row 2, Cell 1');
$table->addCell(1750)->addText('Row 2, Cell 2');
$table->addCell(1750)->addText('Row 2, Cell 3');

// Define a file name for the output document
$filename = 'MyPhpWordDocument.docx';

// Save the document to a file
try {
    $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save($filename);
    echo "Document '{$filename}' created successfully!\n";
} catch (Exception $e) {
    echo 'Error saving document: ' . $e->getMessage() . '\n';
}

/*
To output the document directly to the browser instead of saving to a file, you would typically do this:

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');

$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');
exit;
*/
?>