This project is read-only.

Variable on Footer of Docx Template

Nov 30, 2010 at 7:12 AM

Before Anything else, I would like to congratulate the developers of PHPWord for a very useful Framework that you have made. I am a developer of a Document Processing System, And I was able to used the docx template by inseritng variables to the body of the template. But what I  also need to work is to put variables on footer, I've tried it already a couple of times but it dont work. any help will be deeply appreciated. Thanks in  Advance!

Nov 30, 2010 at 2:18 PM

Here is the footer I use. As you can see, I discovered some 'autotext' worked, and some did not. Is this what you mean by variables?

$paraStyle = array('align' => 'center');
// report footer $footer = $section->createFooter(); $footer->addPreserveText('{ DATE \@"MMMM d, yyyy" } Page {PAGE} of {NUMPAGES}', null, $paraStyle );
//can't seem to insert other 'autotext' like {FILENAME} and {AUTHOR}

Dec 2, 2010 at 8:59 AM

uhmm sorry to bother you again. thanks for the quick reply! What I mean is that I already have a docx template but when I am trying to put a ${variable}  on the footer, it dont work,

Apr 13, 2011 at 4:50 AM

The problem is the template system in PHPWord only looks in the main document, not the header or footer. I customized the Beta 0.6.2, 24.07.2010 version of the PHPWord/Template.php file which contains the PHPWord_Template class to get around this problem. Here's the entire file with my comments (noted by comments with my name in them.

-Matt Bowden

 

<?php
/**
 * PHPWord
 *
 * Copyright (c) 2010 PHPWord
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 010 PHPWord
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    Beta 0.6.2, 24.07.2010
 */


/**
 * PHPWord_DocumentProperties
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 2009 - 2010 PHPWord (http://www.codeplex.com/PHPWord)
 */
class PHPWord_Template {
    
    /**
     * ZipArchive
     * 
     * @var ZipArchive
     */
    private $_objZip;
    
    /**
     * Temporary Filename
     * 
     * @var string
     */
    private $_tempFileName;
    
    /**
     * Document XML
     * 
     * @var string
     */
    private $_documentXML;
    
	/**
     * Header1 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_header1XML;
	
	/**
     * Header2 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_header2XML;
	
	/**
     * Header3 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_header3XML;
    
	/**
     * Footer1 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_footer1XML;
	
	/**
     * Footer2 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_footer2XML;
	
	/**
     * Footer3 XML
	 * Custom code by Matt Bowden (blenderstyle) 04/12/2011
     * 
     * @var string
     */
    private $_footer3XML;
	
    /**
     * Create a new Template Object
     * 
     * @param string $strFilename
     */
    public function __construct($strFilename) {
        $path = dirname($strFilename);
        //$this->_tempFileName = $path.time().'.docx';
        $this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx'; // $path doesn't include the trailing slash - Custom code by Matt Bowden (blenderstyle) 04/12/2011
		
        copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File

        $this->_objZip = new ZipArchive();
        $this->_objZip->open($this->_tempFileName);
        
        $this->_documentXML = $this->_objZip->getFromName('word/document.xml');
		$this->_header1XML = $this->_objZip->getFromName('word/header1.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_header2XML = $this->_objZip->getFromName('word/header2.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_header3XML = $this->_objZip->getFromName('word/header3.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer1XML = $this->_objZip->getFromName('word/footer1.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer2XML = $this->_objZip->getFromName('word/footer2.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer3XML = $this->_objZip->getFromName('word/footer3.xml'); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
    }
    
    /**
     * Set a Template value
     * 
     * @param mixed $search
     * @param mixed $replace
     */
    public function setValue($search, $replace) {
        if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
            $search = '${'.$search.'}';
        }
        
        if(!is_array($replace)) {
            $replace = utf8_encode($replace);
        }
        
        $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
		$this->_header1XML = str_replace($search, $replace, $this->_header1XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_header2XML = str_replace($search, $replace, $this->_header2XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_header3XML = str_replace($search, $replace, $this->_header3XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer1XML = str_replace($search, $replace, $this->_footer1XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer2XML = str_replace($search, $replace, $this->_footer2XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_footer3XML = str_replace($search, $replace, $this->_footer3XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
    }
    
    /**
     * Save Template
     * 
     * @param string $strFilename
     */
    public function save($strFilename) {
        if(file_exists($strFilename)) {
            unlink($strFilename);
        }
        
        $this->_objZip->addFromString('word/document.xml', $this->_documentXML);
		$this->_objZip->addFromString('word/header1.xml', $this->_header1XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_objZip->addFromString('word/header2.xml', $this->_header2XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_objZip->addFromString('word/header3.xml', $this->_header3XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_objZip->addFromString('word/footer1.xml', $this->_footer1XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_objZip->addFromString('word/footer2.xml', $this->_footer2XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
		$this->_objZip->addFromString('word/footer3.xml', $this->_footer3XML); // Custom code by Matt Bowden (blenderstyle) 04/12/2011
        
        // Close zip file
        if($this->_objZip->close() === false) {
            throw new Exception('Could not close zip file.');
        }
        
        rename($this->_tempFileName, $strFilename);
    }
}
?>

Mar 11, 2013 at 3:12 PM
Great Matt!!!

Works fine!
May 2, 2013 at 10:38 AM
This works excellently for me.

Anyone know how to add x number of headers without having to add each one?
Jan 9, 2014 at 4:54 AM
Thanks for you code I used it and add some.

Now you can read all headers and footers.

https://phpword.codeplex.com/workitem/69

if anyone is interested .
Jan 9, 2014 at 4:55 AM
Feb 25, 2014 at 4:43 PM
hi,

I'm utilized the code customized by Matt. But not works for me.
I try define a variable at the header unsuccessfully.
see:

require_once 'PHPWord/PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Planejamento_frete.docx');
$document->setValue('Header1', 'Eduardo Antonio Alves de Souza');
$document->save('/home/sgi3/src/intra/share/SWorkspace/AWorkflamma/ClassTemplate/blocos/SGDN/Novediacv.docx');

could help me
Mar 20, 2014 at 6:08 AM