Template.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_DocumentProperties
  29. *
  30. * @category PHPWord
  31. * @package PHPWord
  32. * @copyright Copyright (c) 2009 - 2011 PHPWord (http://www.codeplex.com/PHPWord)
  33. */
  34. class PHPWord_Template {
  35. /**
  36. * ZipArchive
  37. *
  38. * @var ZipArchive
  39. */
  40. private $_objZip;
  41. /**
  42. * Temporary Filename
  43. *
  44. * @var string
  45. */
  46. private $_tempFileName;
  47. /**
  48. * Document XML
  49. *
  50. * @var string
  51. */
  52. private $_documentXML;
  53. /**
  54. * Create a new Template Object
  55. *
  56. * @param string $strFilename
  57. */
  58. public function __construct($strFilename) {
  59. $path = dirname($strFilename);
  60. $this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';
  61. copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File
  62. $this->_objZip = new ZipArchive();
  63. $this->_objZip->open($this->_tempFileName);
  64. $this->_documentXML = $this->_objZip->getFromName('word/document.xml');
  65. }
  66. /**
  67. * Set a Template value
  68. *
  69. * @param mixed $search
  70. * @param mixed $replace
  71. // $document->setValue('Template', iconv('utf-8', 'GB2312//IGNORE', '中文'));
  72. */
  73. public function setValue($search, $replace) {
  74. if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
  75. $search = '${'.$search.'}';
  76. }
  77. if(!is_array($replace)) {
  78. //$replace = utf8_encode($replace);
  79. //$replace = iconv('gbk', 'utf-8', $replace);
  80. //$replace = iconv('GB2312', 'utf-8//IGNORE', $replace);
  81. $replace = PHPWord_Media::GBKToUTF8($replace);
  82. }
  83. $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
  84. }
  85. /**
  86. * Save Template
  87. *
  88. * @param string $strFilename
  89. */
  90. public function save($strFilename) {
  91. if(file_exists($strFilename)) {
  92. unlink($strFilename);
  93. }
  94. $this->_objZip->addFromString('word/document.xml', $this->_documentXML);
  95. // Close zip file
  96. if($this->_objZip->close() === false) {
  97. throw new Exception('Could not close zip file.');
  98. }
  99. rename($this->_tempFileName, $strFilename);
  100. }
  101. }
  102. ?>