/** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception. * * @param string $documentTemplate The fully qualified template filename. * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * @throws \PhpOffice\PhpWord\Exception\CopyFileException */ public function __construct($documentTemplate) { // Temporary document filename initialization $this->temporaryDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord'); if (false === $this->temporaryDocumentFilename) { throw new CreateTemporaryFileException(); } // Template file cloning if (false === copy($documentTemplate, $this->temporaryDocumentFilename)) { throw new CopyFileException($documentTemplate, $this->temporaryDocumentFilename); } // Temporary document content extraction $this->zipClass = new ZipArchive(); $this->zipClass->open($this->temporaryDocumentFilename); $index = 1; while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) { $this->temporaryDocumentHeaders[$index] = $this->zipClass->getFromName($this->getHeaderName($index)); $index++; } $index = 1; while ($this->zipClass->locateName($this->getFooterName($index)) !== false) { $this->temporaryDocumentFooters[$index] = $this->zipClass->getFromName($this->getFooterName($index)); $index++; } $this->temporaryDocumentMainPart = $this->zipClass->getFromName('word/document.xml'); }
/** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception. * * @param string $documentTemplate The fully qualified template filename. * * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * @throws \PhpOffice\PhpWord\Exception\CopyFileException */ public function __construct($documentTemplate) { // Temporary document filename initialization $this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord'); if (false === $this->tempDocumentFilename) { throw new CreateTemporaryFileException(); } // Template file cloning if (false === copy($documentTemplate, $this->tempDocumentFilename)) { throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); } // Temporary document content extraction $this->zipClass = new ZipArchive(); $this->zipClass->open($this->tempDocumentFilename); $index = 1; while (false !== $this->zipClass->locateName($this->getHeaderName($index))) { $this->tempDocumentHeaders[$index] = $this->fixBrokenMacros($this->zipClass->getFromName($this->getHeaderName($index))); $index++; } $index = 1; while (false !== $this->zipClass->locateName($this->getFooterName($index))) { $this->tempDocumentFooters[$index] = $this->fixBrokenMacros($this->zipClass->getFromName($this->getFooterName($index))); $index++; } $this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml')); $this->temporaryWordRelDocumentPart = $this->zipClass->getFromName('word/_rels/document.xml.rels'); $this->temporaryContentType = $this->zipClass->getFromName('[Content_Types].xml'); // clean the temporary document $this->cleanTemporaryDocument(); }
/** * @covers ::setTempDir * @covers ::getTempDir * @depends testPhpTempDirIsUsedByDefault * @test */ public function testTempDirCanBeSet() { $userDefinedTempDir = 'C:\\PhpWordTemp'; Settings::setTempDir($userDefinedTempDir); $currentTempDir = Settings::getTempDir(); $this->assertEquals($userDefinedTempDir, $currentTempDir); $this->assertNotEquals(sys_get_temp_dir(), $currentTempDir); }
/** * Clear document */ public static function clear() { if (file_exists(self::$file)) { unlink(self::$file); } if (is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) { self::deleteDir(Settings::getTempDir() . '/PhpWord_Unit_Test/'); } }
/** * Test set/get abstract renderer properties */ public function testSetGetAbstractRendererProperties() { define('DOMPDF_ENABLE_AUTOLOAD', false); $rendererName = Settings::PDF_RENDERER_DOMPDF; $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf'); Settings::setPdfRenderer($rendererName, $rendererLibraryPath); $writer = new PDF(new PhpWord()); $writer->setFont('arial'); $this->assertEquals('arial', $writer->getFont()); $writer->setPaperSize(); $this->assertEquals(9, $writer->getPaperSize()); $writer->setOrientation(); $this->assertEquals('default', $writer->getOrientation()); $writer->setTempDir(Settings::getTempDir()); $this->assertEquals(Settings::getTempDir(), $writer->getTempDir()); }
/** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception. * * @param string $documentTemplate The fully qualified template filename. * * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * @throws \PhpOffice\PhpWord\Exception\CopyFileException */ public function __construct($documentTemplate) { // Temporary document filename initialization $this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord'); if (false === $this->tempDocumentFilename) { throw new CreateTemporaryFileException(); } // Template file cloning if (false === copy($documentTemplate, $this->tempDocumentFilename)) { throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); } // Temporary document content extraction $this->zipClass = new ZipArchive(); $this->zipClass->open($this->tempDocumentFilename); // Edited by Luan Nguyen if (! file_exists(BACKEND_V2_TRASH_PATH)) { mkdir(BACKEND_V2_TRASH_PATH, 0777); } $this->zipClass->extractTo(BACKEND_V2_TRASH_PATH); $index = 1; while (false !== $this->zipClass->locateName($this->getHeaderName($index))) { $this->tempDocumentHeaders[$index] = $this->fixBrokenMacros( $this->zipClass->getFromName($this->getHeaderName($index)) ); $index++; } $index = 1; while (false !== $this->zipClass->locateName($this->getFooterName($index))) { $this->tempDocumentFooters[$index] = $this->fixBrokenMacros( $this->zipClass->getFromName($this->getFooterName($index)) ); $index++; } $this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml')); $this->temporaryDocumentRels = $this->zipClass->getFromName('word/_rels/document.xml.rels'); }
/** * Get temporary file name * * If $filename is php://output or php://stdout, make it a temporary file * * @param string $filename * @return string */ protected function getTempFile($filename) { // Temporary directory $this->setTempDir(Settings::getTempDir() . '/PHPWordWriter/'); // Temporary file $this->originalFilename = $filename; if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') { $filename = tempnam(Settings::getTempDir(), 'PhpWord'); if (false === $filename) { $filename = $this->originalFilename; } } $this->tempFilename = $filename; return $this->tempFilename; }
/** * Get image size from archive * * @since 0.12.0 Throws CreateTemporaryFileException. * * @param string $source * @return array|null * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException */ private function getArchiveImageSize($source) { $imageData = null; $source = substr($source, 6); list($zipFilename, $imageFilename) = explode('#', $source); $tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage'); if (false === $tempFilename) { throw new CreateTemporaryFileException(); } $zip = new ZipArchive(); if ($zip->open($zipFilename) !== false) { if ($zip->locateName($imageFilename)) { $imageContent = $zip->getFromName($imageFilename); if ($imageContent !== false) { file_put_contents($tempFilename, $imageContent); $imageData = getimagesize($tempFilename); unlink($tempFilename); } } $zip->close(); } return $imageData; }
<?php include_once 'Sample_Header.php'; use PhpOffice\PhpWord\Settings; $requirements = array('php' => array('PHP 5.3.3', version_compare(PHP_VERSION, '5.3.3', '>=')), 'xml' => array('PHP extension XML', extension_loaded('xml')), 'temp' => array('Temp folder "<code>' . Settings::getTempDir() . '</code>" is writable', is_writable(Settings::getTempDir())), 'zip' => array('PHP extension ZipArchive (optional)', extension_loaded('zip')), 'gd' => array('PHP extension GD (optional)', extension_loaded('gd')), 'xmlw' => array('PHP extension XMLWriter (optional)', extension_loaded('xmlwriter')), 'xsl' => array('PHP extension XSL (optional)', extension_loaded('xsl'))); if (!CLI) { ?> <div class="jumbotron"> <p>Welcome to PHPWord, a library written in pure PHP that provides a set of classes to write to and read from different document file formats, i.e. Office Open XML (.docx), Open Document Format (.odt), and Rich Text Format (.rtf).</p> <p> </p> <p> <a class="btn btn-lg btn-primary" href="https://github.com/PHPOffice/PHPWord" role="button"><i class="fa fa-github fa-lg" title="GitHub"></i> Fork us on Github!</a> <a class="btn btn-lg btn-primary" href="http://phpword.readthedocs.org/" role="button"><i class="fa fa-book fa-lg" title="Docs"></i> Read the Docs</a> </p> </div> <?php } if (!CLI) { echo "<h3>Requirement check:</h3>"; echo "<ul>"; foreach ($requirements as $key => $value) { list($label, $result) = $value; $status = $result ? 'passed' : 'failed'; echo "<li>{$label} ... <span class='{$status}'>{$status}</span></li>"; } echo "</ul>"; include_once 'Sample_Footer.php'; } else { echo 'Requirement check:' . PHP_EOL; foreach ($requirements as $key => $value) { list($label, $result) = $value;
/** * Open a new zip archive * * @param string $filename The file name of the ZIP archive to open * @param int $flags The mode to use to open the archive * @return bool */ public function open($filename, $flags = null) { $result = true; $this->filename = $filename; if (!$this->usePclzip) { $zip = new \ZipArchive(); $result = $zip->open($this->filename, $flags); // Scrutizer will report the property numFiles does not exist // See https://github.com/scrutinizer-ci/php-analyzer/issues/190 $this->numFiles = $zip->numFiles; } else { $zip = new \PclZip($this->filename); $this->tempDir = Settings::getTempDir(); $this->numFiles = count($zip->listContent()); } $this->zip = $zip; return $result; }