From 580a61a832929312cb7bc7fe6aba72fd00ab147f Mon Sep 17 00:00:00 2001
From: Ivan Lanin {$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 .= '