1. Go to this page and download the library: Download d36dak/docx-builder 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/ */
d36dak / docx-builder example snippets
declare(strict_types=1);
DocxBuilder;
use D36Dak\DocxBuilder\Builder\ParagraphBuilder;
$outputPath = __DIR__ . '/example.docx';
$builder = new DocxBuilder([
'format' => 'a4',
'margins' => [
'top' => 1440,
'right' => 1440,
'bottom' => 1440,
'left' => 1440,
],
]);
$builder
->addHeader('Quarterly report', [
'alignment' => 'center',
'color' => '777777',
'fontSize' => 10,
])
->addParagraph('Quarterly report', [
'alignment' => 'center',
'fontSize' => 18,
'bold' => true,
'spacingAfter' => 240,
])
->addParagraph(
(new ParagraphBuilder(['fontSize' => 12]))
->addTextRun('This paragraph uses ')
->addTextRun('multiple text runs', ['bold' => true, 'color' => '0055AA'])
->addTextRun(' inside one paragraph.')
->build()
)
->addParagraph(
'Paragraph options can control alignment, spacing, line height, font family, font size, color, and basic text styling.',
[
'alignment' => 'both',
'lineSpacing' => 1.15,
'spacingAfter' => 200,
]
)
->addTable([
['Metric', 'Current', 'Previous'],
['Revenue', '$120,000', '$98,000'],
['Customers', '340', '295'],
['Conversion', '7.4%', '6.8%'],
], [
'headerRowCount' => 1,
'headerCellOptions' => [
'alignment' => 'center',
'bold' => true,
],
'cellOptions' => [
'fontSize' => 10,
],
])
->addPageBreak()
->addParagraph('Second page', [
'fontSize' => 16,
'bold' => true,
])
->addParagraph('Images can be embedded when a local image file is available.')
->addImage(__DIR__ . '/example-image.png', [
'width' => 200,
'height' => 200,
'alignment' => 'center',
'altText' => 'Example image',
])
->addFirstPageFooter('Generated on ' . date('Y-m-d H:i:s'), [
'fontSize' => 10,
'color' => '777777',
])
->addFooter(
(new ParagraphBuilder(['alignment' => 'right', 'fontSize' => 10]))
->addTextRun('Page ')
->addPageNumber(['bold' => true])
->addTextRun(' of ')
->addTotalPagesNumber(['bold' => true])
->build()
);
// Save the document to disk.
$builder->save($outputPath);
// Or get the generated DOCX contents as a string, for example to return from a web response.
$contents = $builder->getContents();