diff --git a/CHANGELOG.md b/CHANGELOG.md index d6b7bfb5..d8268a73 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.10.0 - Not yet released -This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable. Basic HTML support is enabled. +This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable. Basic HTML and PDF writing support is enabled. ### Features @@ -33,6 +33,7 @@ This release marked heavy refactorings on internal code structure with the creat - ODT Writer: Basic table writing support - @ivanlanin - Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 - HTML Writer: Basic HTML writer initiated - @ivanlanin GH-203 GH-67 GH-147 +- PDF Writer: Basic PDF writer initiated using DomPDF - @ivanlanin GH-68 ### Bugfixes diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index e3057db7..04fd8a40 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -11,6 +11,16 @@ define('IS_INDEX', SCRIPT_FILENAME == 'index'); require_once '../src/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); +// Set writers +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf'); + +// Set PDF renderer +$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF; +$rendererLibraryPath = ""; // Put dompdf library path +if (!\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) { + $writers['PDF'] = null; +} + // Return to the caller script when runs by CLI if (CLI) { return; @@ -22,9 +32,6 @@ $pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - "; $pageTitle .= 'PHPWord'; $pageHeading = IS_INDEX ? '' : "

{$pageHeading}

"; -// Set writers -$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html'); - // Populate samples $files = ''; if ($handle = opendir('.')) { @@ -51,10 +58,15 @@ function write($phpWord, $filename, $writers) // Write foreach ($writers as $writer => $extension) { - $result .= date('H:i:s') . " Write to {$writer} format" . EOL; - $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); - $xmlWriter->save("{$filename}.{$extension}"); - rename("{$filename}.{$extension}", "results/{$filename}.{$extension}"); + $result .= date('H:i:s') . " Write to {$writer} format"; + if (!is_null($extension)) { + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$filename}.{$extension}"); + rename("{$filename}.{$extension}", "results/{$filename}.{$extension}"); + } else { + $result .= ' ... NOT DONE!'; + } + $result .= EOL; } // Do not show execution time for index diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index c15ec821..9f7c8162 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -28,7 +28,7 @@ abstract class IOFactory */ public static function createWriter(PhpWord $phpWord, $name = 'Word2007') { - if (!in_array($name, array('WriterInterface', 'Word2007', 'ODText', 'RTF', 'HTML'))) { + if (!in_array($name, array('WriterInterface', 'Word2007', 'ODText', 'RTF', 'HTML', 'PDF'))) { throw new Exception("\"{$name}\" is not a valid writer."); } diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 0af6cf72..4558736b 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -14,10 +14,13 @@ namespace PhpOffice\PhpWord; */ class Settings { - /** Available Zip library classes */ + /** Available Zip library classes */ const PCLZIP = 'PhpOffice\\PhpWord\\Shared\\ZipArchive'; const ZIPARCHIVE = 'ZipArchive'; + /** Optional PDF Rendering libraries */ + const PDF_RENDERER_DOMPDF = 'DomPDF'; + /** * Compatibility option for XMLWriter * @@ -27,13 +30,32 @@ class Settings /** * Name of the class used for Zip file management - * e.g. - * ZipArchive * * @var string */ private static $zipClass = self::ZIPARCHIVE; + /** + * Name of the classes used for PDF renderer + * + * @var array + */ + private static $pdfRenderers = array(self::PDF_RENDERER_DOMPDF); + + /** + * Name of the external Library used for rendering PDF files + * + * @var string + */ + private static $pdfRendererName = null; + + /** + * Directory Path to the external Library used for rendering PDF files + * + * @var string + */ + private static $pdfRendererPath = null; + /** * Set the compatibility option used by the XMLWriter * @@ -74,7 +96,7 @@ class Settings return true; } return false; - } // function setZipClass() + } /** * Return the name of the Zip handler Class that PHPWord is configured to use (PCLZip or ZipArchive) @@ -87,5 +109,71 @@ class Settings public static function getZipClass() { return self::$zipClass; - } // function getZipClass() + } + + /** + * Set details of the external library for rendering PDF files + * + * @param string $libraryName + * @param string $libraryBaseDir + * @return boolean Success or failure + */ + public static function setPdfRenderer($libraryName, $libraryBaseDir) + { + if (!self::setPdfRendererName($libraryName)) { + return false; + } + + return self::setPdfRendererPath($libraryBaseDir); + } + + /** + * Return the PDF Rendering Library + */ + public static function getPdfRendererName() + { + return self::$pdfRendererName; + } + + /** + * Identify the external library to use for rendering PDF files + * + * @param string $libraryName + * @return boolean Success or failure + */ + public static function setPdfRendererName($libraryName) + { + if (!in_array($libraryName, self::$pdfRenderers)) { + return false; + } + + self::$pdfRendererName = $libraryName; + + return true; + } + + + /** + * Return the directory path to the PDF Rendering Library + */ + public static function getPdfRendererPath() + { + return self::$pdfRendererPath; + } + + /** + * Location of external library to use for rendering PDF files + * + * @param string $libraryBaseDir Directory path to the library's base folder + * @return boolean Success or failure + */ + public static function setPdfRendererPath($libraryBaseDir) + { + if ((file_exists($libraryBaseDir) === false) || (is_readable($libraryBaseDir) === false)) { + return false; + } + self::$pdfRendererPath = $libraryBaseDir; + + return true; + } } diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 797dd9dc..f0879332 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -16,6 +16,7 @@ use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\ListItem; use PhpOffice\PhpWord\Element\Object; use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\PreserveText; use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextBreak; @@ -65,7 +66,7 @@ class HTML extends AbstractWriter implements WriterInterface * * @return string */ - private function writeDocument() + public function writeDocument() { $html = ''; $html .= '' . PHP_EOL; @@ -87,7 +88,7 @@ class HTML extends AbstractWriter implements WriterInterface * * @return string */ - public function writeHTMLHead() + private function writeHTMLHead() { $properties = $this->getPhpWord()->getDocumentProperties(); $propertiesMapping = array( @@ -124,7 +125,7 @@ class HTML extends AbstractWriter implements WriterInterface * * @return string */ - public function writeHTMLBody() + private function writeHTMLBody() { $phpWord = $this->getPhpWord(); $html = ''; @@ -136,8 +137,8 @@ class HTML extends AbstractWriter implements WriterInterface if ($countSections > 0) { foreach ($sections as $section) { $pSection++; - $cellContents = $section->getElements(); - foreach ($cellContents as $element) { + $contents = $section->getElements(); + foreach ($contents as $element) { if ($element instanceof Text) { $html .= $this->writeText($element); } elseif ($element instanceof TextRun) { @@ -161,9 +162,9 @@ class HTML extends AbstractWriter implements WriterInterface } elseif ($element instanceof Object) { $html .= $this->writeObject($element); } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element, true); + $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { - $html .= $this->writeEndnote($element, true); + $html .= $this->writeEndnote($element); } } } @@ -248,9 +249,9 @@ class HTML extends AbstractWriter implements WriterInterface } elseif ($element instanceof Image) { $html .= $this->writeImage($element, true); } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element, true); + $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { - $html .= $this->writeEndnote($element, true); + $html .= $this->writeEndnote($element); } } $html .= '

' . PHP_EOL; @@ -263,6 +264,7 @@ class HTML extends AbstractWriter implements WriterInterface * Write link * * @param Link $element + * @param boolean $withoutP * @return string */ private function writeLink($element, $withoutP = false) @@ -300,6 +302,7 @@ class HTML extends AbstractWriter implements WriterInterface * Write preserve text * * @param PreserveText $element + * @param boolean $withoutP * @return string */ private function writePreserveText($element, $withoutP = false) @@ -350,7 +353,7 @@ class HTML extends AbstractWriter implements WriterInterface /** * Write table * - * @param Title $element + * @param Table $element * @return string */ private function writeTable($element) @@ -361,7 +364,7 @@ class HTML extends AbstractWriter implements WriterInterface if ($cRows > 0) { $html .= "" . PHP_EOL; foreach ($rows as $row) { - $height = $row->getHeight(); + // $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $html .= "" . PHP_EOL; @@ -388,13 +391,13 @@ class HTML extends AbstractWriter implements WriterInterface } elseif ($content instanceof Object) { $html .= $this->writeObject($content); } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element, true); + $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { - $html .= $this->writeEndnote($element, true); + $html .= $this->writeEndnote($element); } } } else { - $this->writeTextBreak($content); + $html .= $this->writeTextBreak(new TextBreak()); } $html .= "" . PHP_EOL; } @@ -410,6 +413,7 @@ class HTML extends AbstractWriter implements WriterInterface * Write image * * @param Image $element + * @param boolean $withoutP * @return string */ private function writeImage($element, $withoutP = false) @@ -421,6 +425,7 @@ class HTML extends AbstractWriter implements WriterInterface * Write object * * @param Object $element + * @param boolean $withoutP * @return string */ private function writeObject($element, $withoutP = false) @@ -478,11 +483,14 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeStyles() { + $bodyCss = array(); $css = '