PHP code example of philgale92 / docx
1. Go to this page and download the library: Download philgale92/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/ */
philgale92 / docx example snippets
/*
* Create the parse object which converts the file into internalised objects
*/
$parser = new \PhilGale92Docx\Docx($absolutePathToDocxFile );
/*
* Attach style info (if any)
*/
$parser->addStyle(
(new \PhilGale92Docx\Style())
->setStyleId('standardPara')
->setHtmlClass('custom')
->setHtmlTag('p') // 'p' is default behaviour
);
/*
* Here is an example of a MetaData attribute style
* (Lets pull out the titleStyle directly)
*/
$parser->addStyle(
(new \PhilGale92Docx\Style())
->setStyleId('0TitleName')
// By setting this as metaData, we can pull in
// any content where this style is used in a seperate call.
// It also removes the content from the standard render
->setIsMetaData(true)
// By default the metaData is parsed as HTML
// But if you need to get the literal plain text then we can do that too
->setMetaDataRenderMode(\PhilGale92Docx\Docx::RENDER_MODE_PLAIN)
);
/*
* Here is an example of a heading style
*/
$parser->addStyle(
(new \PhilGale92Docx\Style())
->setStyleId('1HeadingStyle')
->setHtmlTag('h2')
->setHtmlClass('custom')
);
/*
* Here is an example where we want to wrap all adjacent styles
* of this name with a div
*/
$parser->addStyle(
(new \PhilGale92Docx\Style())
->setStyleId('3Boxgreytint')
->setBoxSimilarSiblings(true) // enable boxing behaviour
->setBoxClassName('box-style-tint-grey') // class of wrapping div
);
/*
* You can also create word styles that make text lists
* this is in addition to the standard list detection
*/
$parser->addStyle(
(new \PhilGale92Docx\Style())
->setStyleId('4Numberedlist')
->setListHtmlTag('ol') // Takes 'ul' or 'ol'. 'ul' is default behaviour
->setListLevel(1) // the indentation level, must be > 0
);
/*
* Now parse the xml into internal objects
*/
$parser->parse(); // Optional, ran automatically by ->render() if not ran yet
/*
* Now render the parser into html string
*/
echo $parser
->render(\PhilGale92Docx\Docx::RENDER_MODE_HTML)
;
/*
* We can grab any metaData content after ->parse() is performed
* ->getMetaData() runs ->parse() if not ran yet
*/
var_dump(
$parser->getMetaData() // also takes $styleId of metaData as an argument
);
/*
* Are there any styles we forgot to declare?
*/
var_dump($parser->getDetectedStyles());