Skip to content

Instantly share code, notes, and snippets.

@CauanCabral
Last active January 3, 2022 14:25
Show Gist options
  • Save CauanCabral/723e9c1c91faa980f539 to your computer and use it in GitHub Desktop.
Save CauanCabral/723e9c1c91faa980f539 to your computer and use it in GitHub Desktop.
PHPWord ToC
<?php
require('Vendor/autoload.php');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Calibri');
$phpWord->setDefaultFontSize(14);
$phpWord->addFontStyle('normal', array(
'color' => '000000',
'bold' => false,
'italic' => false
));
$phpWord->addFontStyle('title', array(
'color' => '280099',
'size' => 32,
'bold' => true
));
$phpWord->addFontStyle('identifier', array(
'size' => 11,
'bold' => true
));
$phpWord->addFontStyle('destaque', array(
'color' => '000000',
'size' => 15,
'bold' => true
));
$phpWord->addFontStyle('assinatura', array(
'size' => 11
));
$phpWord->addFontStyle('abre-section', array(
'color' => '280099',
'size' => 19
));
$phpWord->addParagraphStyle('p-abre-section', array(
'align' => 'center',
'spaceBefore' => \PhpOffice\PhpWord\Shared\Font::centimeterSizeToTwips('7.7'),
'borderBottomSize' => 2,
'borderColor' => '000000'
));
$section = $phpWord->addSection();
$phpWord->addSection(['pageNumberingStart' => 0]);
$footer = $section->addFooter();
$footer->addPreserveText("Report footer \t\t\t\t\t\t\t\t\t {PAGE}", 'assinatura', array('align'=>'justify'));
$section->addText("\n\n\n ", 'title');
$section->addText("Docx Lorem Ipsum", 'normal');
$section->addText("Some Text as Title", 'title');
$section->addText("\n ", 'title');
$section->addText('Docx From – ' . date('F') . ' de ' . date('Y'), 'destaque');
$section->addText("\n ", 'normal');
$section->addText("\n", 'title');
$section->addText('Gerado: ' . date('d/m/Y H:i'), 'assinatura');
$section->addPageBreak();
$section->addText('Table of Content', 'title');
$section->addToc();
$section = $phpWord->addSection();
$section->addText('Special Text', 'abre-section', 'p-abre-section');
$section->addTitle('Título 1', 1);
$section->addPageBreak();
$section = $phpWord->addSection();
$section->addTitle('Titulo 2', 1);
$section->addTitle('Subtitulo 2', 2);
$section->addPageBreak();
$phpWord->save('tmp/mydoc.docx');
// output: https://drive.google.com/file/d/0B2B1RtEnPJacblEwZTlnU3pVVTg/
// output converted to PDF by LibreOffice: https://drive.google.com/file/d/0B2B1RtEnPJaceTVxa241aG53OFU/
<?php
require('Vendor/autoload.php');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Begin code
$section = $phpWord->addSection();
// Define the TOC font style
$fontStyle = array('spaceAfter' => 60, 'size' => 12);
$fontStyle2 = array('size' => 10);
// Add title styles
$phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
$phpWord->addTitleStyle(2, array('size' => 16, 'color' => '666666'));
$phpWord->addTitleStyle(3, array('size' => 14, 'italic' => true));
$phpWord->addTitleStyle(4, array('size' => 12));
// Add text elements
$section->addText('Table of contents 1');
$section->addTextBreak(2);
// Add TOC #1
$toc = $section->addTOC($fontStyle);
$section->addTextBreak(2);
// Filler
$section->addText('Text between TOC');
$section->addTextBreak(2);
// Add TOC #1
$section->addText('Table of contents 2');
$section->addTextBreak(2);
$toc2 = $section->addTOC($fontStyle2);
$toc2->setMinDepth(2);
$toc2->setMaxDepth(3);
// Add Titles
$section->addPageBreak();
$section->addTitle('I am Title 1', 1);
$section->addText('Some text...');
$section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 1', 2);
$section->addTextBreak(2);
$section->addText('Some more text...');
$section->addTextBreak(2);
$section->addTitle('Another Title (Title 2)', 1);
$section->addText('Some text...');
$section->addPageBreak();
$section->addTitle('I am Title 3', 1);
$section->addText('And more text...');
$section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 3', 2);
$section->addText('Again and again, more text...');
$section->addTitle('Subtitle 3.1.1', 3);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.1.1', 4);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.1.2', 4);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.2', 3);
$section->addText('Text');
echo $phpWord->save('tmp/sample.docx') ? 'Created' : 'Fail';
// output: https://docs.google.com/document/d/0B2B1RtEnPJacdl9IRENVUnNKZEU/edit?usp=sharing&ouid=115014919096499709829&resourcekey=0-EbAdvrhoIkmGoGhitdPQMA&rtpof=true&sd=true
// output converted to PDF by LibreOffice: https://drive.google.com/file/d/0B2B1RtEnPJacLUN1RTVCT3dCVGM/
@chteancitrusbug
Copy link

I need to create TOC with Bidirectional in Hebrew.
How can we do that?

@CauanCabral
Copy link
Author

Hi,

Sorry, I never did this and I not using PHPWord for a long time.

@chteancitrusbug
Copy link

Have you any idea that in which library i can find this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment