$this->archive->open( … ); $this->archive->addFile( … ); $this->archive->close( .. );
// MS Office class OfficeDocument extends ZipArchive{ __construct($filename, $template_path = '/template/' ); protected function add_rels( $filename, $rels, $path = '' ); protected function pparse( $replace, $content ); } // MS Word class WordDocument extends OfficeDocument{ public function __construct( $filename, $template_path = '/template/' ) // , API public function assign( $content = '', $return = false ); public function create(); }
// MS Office $this->rels = array_merge( $this->rels, array( 'rId3' => array( 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 'docProps/app.xml' ), 'rId2' => array( 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 'docProps/core.xml' ), ) );
// protected function add_rels( $filename, $rels, $path = '' ){ // XML $xmlstring = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'; // foreach( $rels as $rId => $params ){ // , . , $pathfile = empty( $params[2] ) ? $this->path . $path . $params[1] : $params[2]; // if( $this->addFile( $pathfile , $path . $params[1] ) === false ) die(' ' . $path . $params[1] ); // $xmlstring .= '<Relationship Id="' . $rId . '" Type="' . $params[0] . '" Target="' . $params[1] . '"/>'; } $xmlstring .= '</Relationships>'; // $this->addFromString( $path . $filename, $xmlstring ); }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> </Relationships>
<w:pw:rsidR="000E3348" w:rsidRDefault="00CD6FED"> <w:r> <w:rPr> <w:noProof/> <w:lang w:eastAsia="ru-RU"/> </w:rPr> <w:drawing> <wp:inline distT="0" distB="0" distL="0" distR="0"> <wp:extent cx="{WIDTH}" cy="{HEIGHT}"/> <wp:effectExtent l="19050" t="0" r="0" b="0"/> <wp:docPr id="2" name=" 2"/> <wp:cNvGraphicFramePr> <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/> </wp:cNvGraphicFramePr> <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"> <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> <pic:nvPicPr> <pic:cNvPr id="0" name="image.jpg"/> <pic:cNvPicPr/> </pic:nvPicPr> <pic:blipFill> <a:blip r:embed="{RID}"> <a:extLst> <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"> <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/> </a:ext> </a:extLst> </a:blip> <a:stretch> <a:fillRect/> </a:stretch> </pic:blipFill> <pic:spPr> <a:xfrm> <a:off x="0" y="0"/> <a:ext cx="{WIDTH}" cy="{HEIGHT}"/> </a:xfrm> <a:prstGeom prst="rect"> <a:avLst/> </a:prstGeom> <a:noFill/> <a:ln> <a:noFill/> </a:ln> </pic:spPr> </pic:pic> </a:graphicData> </a:graphic> </wp:inline> </w:drawing> </w:r> </w:p>
public function assign( $content = '', $return = false ){ // , $text . , if( is_file( $content ) ){ // $block = file_get_contents( $this->path . 'image.xml' ); list( $width, $height ) = getimagesize( $content ); $rid = "rId" . count( $this->word_rels ) . 'i'; $this->word_rels[$rid] = array( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "media/" . $content, // $content ); $xml = $this->pparse( array( '{WIDTH}' => $width * $this->px_emu, '{HEIGHT}' => $height * $this->px_emu, '{RID}' => $rid, ), $block ); } else{ // $block = file_get_contents( $this->path . 'p.xml' ); $xml = $this->pparse( array( '{TEXT}' => $content, ), $block ); } // , XML, if( $return ) return $xml; else $this->content .= $xml; }
$rid = "rId" . count( $this->word_rels ) . 'i'; $this->word_rels[$rid] = array( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "media/" . $content, // $content );
// include 'PHPDocx_0.9.2.php'; // . $w = new WordDocument( ".docx" ); // assign /****************************** / / $w->assign( 'text' ); / $w->assign( 'image.png' ); / $xml = $w->assign( 'image.png', true ); / $w->assign( $w->assign( 'image.png', true ) ); / /******************************/ $w->assign('image.jpg'); $w->assign(' - .'); $w->create();
Source: https://habr.com/ru/post/140012/
All Articles