1. Go to this page and download the library: Download dskripchenko/php-docx library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dskripchenko / php-docx example snippets
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use Dskripchenko\PhpDocx\Reader\DocxReader;
use Dskripchenko\PhpDocx\Html\Serializer;
$phpWord = new PhpWord;
$section = $phpWord->addSection();
$section->addTitle('Quarterly report', 1);
$section->addText('Built with PHPWord, exported to HTML by php-docx.');
$tmp = tempnam(sys_get_temp_dir(), 'docx');
IOFactory::createWriter($phpWord, 'Word2007')->save($tmp);
$document = (new DocxReader)->read((string) file_get_contents($tmp));
$html = (new Serializer)->serialize($document)->bodyHtml;
use Dskripchenko\PhpDocx\Html\Converter;
use Dskripchenko\PhpDocx\Writer\Word2007Writer;
$html = <<<HTML
<h1>Invoice #42</h1>
<p>Total: <strong>500 USD</strong></p>
<table>
<tr><th>Item</th><th>Qty</th></tr>
<tr><td>Widget</td><td>2</td></tr>
</table>
<p>Page <page-number/> of <page-total/></p>
HTML;
$doc = (new Converter)->fromHtml($html);
file_put_contents('invoice.docx', (new Word2007Writer)->write($doc));
use Dskripchenko\PhpDocx\Reader\DocxReader;
use Dskripchenko\PhpDocx\Reader\DocxPackageReader;
use Dskripchenko\PhpDocx\Reader\VariableDetector;
use Dskripchenko\PhpDocx\Html\Serializer;
$bytes = file_get_contents('input.docx');
$document = (new DocxReader)->read($bytes);
$pkg = (new DocxPackageReader)->read($bytes);
$variables = (new VariableDetector)->detect($pkg);
$imported = (new Serializer)->serialize($document, $variables);
echo $imported->bodyHtml;
echo $imported->headerHtml;
echo $imported->footerHtml;
echo $imported->watermarkText;
$imported->pageSettings;
$imported->variables;
$imported->media;
use Dskripchenko\PhpDocx\Style\StyleRegistry;
use Dskripchenko\PhpDocx\Style\RunStyle;
use Dskripchenko\PhpDocx\Style\ParagraphStyle;
use Dskripchenko\PhpDocx\Style\Alignment;
$styles = (new StyleRegistry)
->heading(1, new RunStyle(sizeHalfPoints: 44, bold: true), new ParagraphStyle(alignment: Alignment::Center))
->heading(2, new RunStyle(sizeHalfPoints: 28, bold: true));
$writer = new Word2007Writer($styles);
use Dskripchenko\PhpDocx\Html\Converter;
$doc = (new Converter)->fromHtmlWithStyles($htmlWithStyleBlocks);
// or: new Converter(preprocessor: new MyPreprocessor)
use Dskripchenko\PhpDocx\Build\DocumentBuilder;
use Dskripchenko\PhpDocx\Style\CoreProperties;
use Dskripchenko\PhpDocx\Style\PageSetup;
use Dskripchenko\PhpDocx\Style\PaperSize;
use Dskripchenko\PhpDocx\Style\Orientation;
$doc = DocumentBuilder::new()
->pageSetup(new PageSetup(
paperSize: PaperSize::A4,
orientation: Orientation::Portrait,
))
->watermark('CONFIDENTIAL')
->coreProperties(new CoreProperties(title: 'Report', creator: 'Acme Inc.'))
->heading(1, 'Report')
->paragraph('Body')
->build(); // → Document AST
$bytes = DocumentBuilder::new()->paragraph('Hi')->toBytes();
$count = DocumentBuilder::new()->paragraph('Hi')->toFile('out.docx');
->paragraph(fn ($p) => $p
->alignCenter() // or alignRight()/alignJustify()
->styleId('Quote') // a style id from the StyleRegistry
->pageBreakBefore() // start on a new page; widowControl(false) turns widow control off
->indentMm(left: 20, firstLine: 10)
->spacingPt(before: 6, after: 12)
->text('Indented & spaced')
)
use Dskripchenko\PhpDocx\Build\ListBuilder;
use Dskripchenko\PhpDocx\Element\ListFormat;
->bulletList(fn (ListBuilder $l) => $l
->item('First')
->item('Second', fn ($n) => $n
->item('Nested A')
->item('Nested B')
)
)
->orderedList(fn (ListBuilder $l) => $l
->format(ListFormat::LowerLetter) // a, b, c
->startAt(3)
->item('item starts at "c"')
)
RunStyleBuilder::new()
->bold()
->italic()
->underline()
->strike()
->color('ff0000')
->backgroundColor('eeeeee')
->highlight('yellow')
->fontFamily('Arial')
->fontFamilyEastAsia('SimSun') // w:eastAsia: the font for CJK text
->fontSizePt(14.5)
->build();
use Dskripchenko\PhpDocx\Element\ImageFormat;
use Dskripchenko\PhpDocx\Style\HorizontalAnchor;
use Dskripchenko\PhpDocx\Style\ImagePosition;
use Dskripchenko\PhpDocx\Style\ImageWrap;
use Dskripchenko\PhpDocx\Style\PositionAlign;
use Dskripchenko\PhpDocx\Style\VerticalAnchor;
DocumentBuilder::new()
// A logo in the top right corner of every page, behind the text.
->header(fn ($h) => $h->paragraph(fn ($p) => $p->imageFromFile('logo.png', 120, 40, 'Logo', new ImagePosition(
horizontalFrom: HorizontalAnchor::Page,
horizontalAlign: PositionAlign::Right,
verticalFrom: VerticalAnchor::Page,
yEmu: 360000, // 1 cm from the top
behindText: true,
))))
// A photo the text wraps around.
->paragraph(fn ($p) => $p
->imageFromFile('photo.jpg', position: new ImagePosition(wrap: ImageWrap::Square))
->text('Text flows around the photo.'))
->toFile('floating.docx');
use Dskripchenko\PhpDocx\Build\DocumentBuilder;
use Dskripchenko\PhpDocx\Style\Orientation;
use Dskripchenko\PhpDocx\Style\PageSetup;
use Dskripchenko\PhpDocx\Style\SectionStart;
DocumentBuilder::new()
->header(fn ($h) => $h->paragraph('Report'))
->paragraph('Portrait pages')
->newSection(new PageSetup(orientation: Orientation::Landscape))
->header(fn ($h) => $h->paragraph('Appendix'))
->paragraph('A wide table on landscape pages')
->newSection(new PageSetup(widthTwips: 8391, heightTwips: 11906), SectionStart::OddPage)
->paragraph('A custom page size (A5 here), starting on an odd page')
->toFile('sections.docx');
use Dskripchenko\PhpDocx\Reader\VariableDetector;
$pkg = (new DocxPackageReader)->read($bytes);
$detector = new VariableDetector; // defaults
// Or with custom regexes:
$detector = new VariableDetector(['/\[\[(\w+)\]\]/']);
$variables = $detector->detect($pkg);
foreach ($variables as $v) {
echo "{$v->name} ({$v->source->value})";
echo " placeholder='{$v->placeholder}'";
echo " sample='{$v->sampleValue}'\n";
}
$bytes1 = file_get_contents('original.docx');
$doc = (new DocxReader)->read($bytes1);
$bytes2 = (new Word2007Writer)->write($doc);
file_put_contents('roundtrip.docx', $bytes2);