From 6839ee41dd0d0fe0d7d3286592c24e2e8446db18 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 8 Jun 2014 20:11:44 +0700 Subject: [PATCH] New `PhpWord::save()` method to encapsulate `IOFactory` --- CHANGELOG.md | 1 + README.md | 13 ++++------ docs/general.rst | 13 ++++------ docs/src/documentation.md | 13 ++++------ samples/Sample_Header.php | 12 ++++------ src/PhpWord/PhpWord.php | 37 +++++++++++++++++++++++++++++ tests/PhpWord/Tests/PhpWordTest.php | 12 ++++++++++ 7 files changed, 67 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19d99348..7d4010ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme - Paragraph: Added shading to the paragraph style for full width shading - @lrobert GH-264 - RTF Writer: Support for sections, margins, and borders - @ivanlanin GH-249 - Section: Ability to set paper size, e.g. A4, A3, and Legal - @ivanlanin GH-249 +- General: New `PhpWord::save()` method to encapsulate `IOFactory` - @ivanlanin ### Bugfixes diff --git a/README.md b/README.md index fefde4eb..a9006f2c 100644 --- a/README.md +++ b/README.md @@ -100,15 +100,10 @@ $fontStyle->setSize(22); $myTextElement = $section->addText('Hello World!'); $myTextElement->setFontStyle($fontStyle); -// Finally, write the document: -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); -$objWriter->save('helloWorld.docx'); - -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); -$objWriter->save('helloWorld.odt'); - -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF'); -$objWriter->save('helloWorld.rtf'); +// Finally, save the document: +$phpWord->save('helloWorld.docx'); +$phpWord->save('helloWorld.odt', 'ODText'); +$phpWord->save('helloWorld.rtf', 'RTF'); ``` ## Known issues diff --git a/docs/general.rst b/docs/general.rst index 270608f7..0bbeb2ec 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -43,15 +43,10 @@ folder `__. $myTextElement = $section->addText('Hello World!'); $myTextElement->setFontStyle($fontStyle); - // Finally, write the document: - $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); - $objWriter->save('helloWorld.docx'); - - $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); - $objWriter->save('helloWorld.odt'); - - $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF'); - $objWriter->save('helloWorld.rtf'); + // Finally, save the document: + $phpWord->save('helloWorld.docx'); + $phpWord->save('helloWorld.odt', 'ODText'); + $phpWord->save('helloWorld.rtf', 'RTF'); Settings -------- diff --git a/docs/src/documentation.md b/docs/src/documentation.md index c76ec1f2..c2fc5e77 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -238,15 +238,10 @@ $fontStyle->setSize(22); $myTextElement = $section->addText('Hello World!'); $myTextElement->setFontStyle($fontStyle); -// Finally, write the document: -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); -$objWriter->save('helloWorld.docx'); - -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); -$objWriter->save('helloWorld.odt'); - -$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF'); -$objWriter->save('helloWorld.rtf'); +// Finally, save the document: +$phpWord->save('helloWorld.docx'); +$phpWord->save('helloWorld.odt', 'ODText'); +$phpWord->save('helloWorld.rtf', 'RTF'); ``` ## Settings diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index 7af23c94..d915c039 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -4,7 +4,6 @@ */ use PhpOffice\PhpWord\Autoloader; use PhpOffice\PhpWord\Settings; -use PhpOffice\PhpWord\IOFactory; error_reporting(E_ALL); define('CLI', (PHP_SAPI == 'cli') ? true : false); @@ -59,12 +58,11 @@ function write($phpWord, $filename, $writers) $result = ''; // Write documents - foreach ($writers as $writer => $extension) { - $result .= date('H:i:s') . " Write to {$writer} format"; - if (!is_null($extension)) { - $xmlWriter = IOFactory::createWriter($phpWord, $writer); - $xmlWriter->save(__DIR__ . "/{$filename}.{$extension}"); - rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}"); + foreach ($writers as $format => $extension) { + $result .= date('H:i:s') . " Write to {$format} format"; + if ($extension !== null) { + $targetFile = __DIR__ . "/results/{$filename}.{$extension}"; + $phpWord->save($targetFile, $format); } else { $result .= ' ... NOT DONE!'; } diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 9c8d5a90..8e889cce 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -260,6 +260,43 @@ class PhpWord } } + /** + * Save to file or download + * + * All exceptions should already been handled by the writers + * + * @param string $filename + * @param string $format + * @param bool $download + * @return bool + */ + public function save($filename, $format = 'Word2007', $download = false) + { + $mime = array( + 'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'ODT' => 'application/vnd.oasis.opendocument.text', + 'RTF' => 'application/rtf', + 'HTML' => 'text/html', + 'PDF' => 'application/pdf', + ); + + $writer = IOFactory::createWriter($this, $format); + + if ($download === true) { + header("Content-Description: File Transfer"); + header('Content-Disposition: attachment; filename="' . $filename . '"'); + header('Content-Type: ' . $mime[$format]); + header('Content-Transfer-Encoding: binary'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Expires: 0'); + $filename = 'php://output'; // Change filename to force download + } + + $writer->save($filename); + + return true; + } + /** * Create new section * diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 6a9e6b26..ca49bf28 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -158,4 +158,16 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->loadTemplate($templateFqfn); } + + /** + * Test save + */ + public function testSave() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $section->addText('Hello world!'); + + $this->assertTrue($phpWord->save('test.docx', 'Word2007', true)); + } }