class Word extends ZipArchive{ // private $files; // public $path; public function __construct($filename, $template_path = '/template/' ){ // $this->path = dirname(__FILE__) . $template_path; // , . if ($this->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { die("Unable to open <$filename>\n"); } // $this->files = array( "word/_rels/document.xml.rels", "word/theme/theme1.xml", "word/fontTable.xml", "word/settings.xml", "word/styles.xml", "word/document.xml", "word/stylesWithEffects.xml", "word/webSettings.xml", "_rels/.rels", "docProps/app.xml", "docProps/core.xml", "[Content_Types].xml" ); // foreach( $this->files as $f ) $this->addFile($this->path . $f , $f ); } // public function create(){ $this->close(); } } $w = new Word( "Example.docx" ); $w->create();
<w:pw:rsidR="00BB20FC" w:rsidRPr="00357A74" w:rsidRDefault="00357A74" w:rsidP="00BB20FC"> <w:pPr> <w:jc w:val="left"/> <w:rPr> <w:sz w:val="28"/> <w:lang w:val="en-US"/> </w:rPr> </w:pPr> <w:rw:rsidRPr="00357A74"> <w:rPr> <w:sz w:val="28"/> <w:lang w:val="en-US"/> </w:rPr> <w:t>{TEXT}</w:t> </w:r> </w:p>
class Word extends ZipArchive{ // private $files; // public $path; // protected $content; public function __construct($filename, $template_path = '/template/' ){ // $this->path = dirname(__FILE__) . $template_path; // , . if ($this->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { die("Unable to open <$filename>\n"); } // $this->files = array( "word/_rels/document.xml.rels", "word/theme/theme1.xml", "word/fontTable.xml", "word/settings.xml", "word/styles.xml", "word/stylesWithEffects.xml", "word/webSettings.xml", "_rels/.rels", "docProps/app.xml", "docProps/core.xml", "[Content_Types].xml" ); // foreach( $this->files as $f ) $this->addFile($this->path . $f , $f ); } // public function assign( $text = '' ){ // $p = file_get_contents( $this->path . 'p.xml' ); // $text_array = explode( "\n", $text ); foreach( $text_array as $str ) $this->content .= str_replace( '{TEXT}', $str, $p ); } // public function create(){ // $this->addFromString("word/document.xml", str_replace( '{CONTENT}', $this->content, file_get_contents( $this->path . "word/document.xml" ) ) ); $this->close(); } } $w = new Word( ".docx" ); $w->assign(' . .'); $w->create();
Source: https://habr.com/ru/post/138666/
All Articles