diff --git a/.gitignore b/.gitignore
index 01606063..810a7b0a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,7 @@ Thumbs.db
Desktop.ini
composer.phar
phpunit.xml
+phpword.ini
/.buildpath
/.idea
/.project
diff --git a/.travis.yml b/.travis.yml
index 02065d10..d4f511da 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -52,7 +52,7 @@ script:
## PHPUnit
- phpunit -c ./ --coverage-text --coverage-html ./build/coverage
## PHPDocumentor
- - vendor/bin/phpdoc.php -d ./src -t ./build/docs --ignore "*/src/PhpWord/Shared/PCLZip/*" --template="responsive-twig"
+ - vendor/bin/phpdoc.php -d ./src -t ./build/docs --ignore "*/src/PhpWord/Shared/*/*" --template="responsive-twig"
after_script:
## PHPDocumentor
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3bed3012..4f687b9d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196
- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin
+- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200
### Bugfixes
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 09524450..fb511bd2 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -10,7 +10,7 @@
syntaxCheck="false">
- ./tests/PhpWord/
+ ./tests/PhpWord
@@ -18,13 +18,11 @@
./src
./src/PhpWord/Shared/PCLZip
+ ./src/PhpWord/Shared/Spyc
-
diff --git a/phpword.ini.dist b/phpword.ini.dist
new file mode 100644
index 00000000..4a51ee11
--- /dev/null
+++ b/phpword.ini.dist
@@ -0,0 +1,14 @@
+; Default config file for PHPWord
+; Copy this file into phpword.ini and use Settings::loadConfig to load
+
+[General]
+
+compatibility = true
+zipClass = ZipArchive
+pdfRendererName = DomPDF
+pdfRendererPath =
+
+[Font]
+
+defaultFontName = Arial
+defaultFontSize = 10
diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php
index e7a24b19..fdada151 100644
--- a/samples/Sample_Header.php
+++ b/samples/Sample_Header.php
@@ -2,6 +2,10 @@
/**
* Header file
*/
+use PhpOffice\PhpWord\Autoloader;
+use PhpOffice\PhpWord\Settings;
+use PhpOffice\PhpWord\IOFactory;
+
error_reporting(E_ALL);
define('CLI', (PHP_SAPI == 'cli') ? true : false);
define('EOL', CLI ? PHP_EOL : '
');
@@ -9,16 +13,14 @@ define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
define('IS_INDEX', SCRIPT_FILENAME == 'index');
require_once '../src/PhpWord/Autoloader.php';
-\PhpOffice\PhpWord\Autoloader::register();
+Autoloader::register();
+Settings::loadConfig();
// 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 = ''; // DomPDF library path
-
-if (!\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) {
+if (Settings::getPdfRendererPath() === null) {
$writers['PDF'] = null;
}
@@ -60,7 +62,7 @@ function write($phpWord, $filename, $writers)
foreach ($writers as $writer => $extension) {
$result .= date('H:i:s') . " Write to {$writer} format";
if (!is_null($extension)) {
- $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer);
+ $xmlWriter = IOFactory::createWriter($phpWord, $writer);
$xmlWriter->save("{$filename}.{$extension}");
rename("{$filename}.{$extension}", "results/{$filename}.{$extension}");
} else {
diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php
index a56578b0..a3abb869 100644
--- a/src/PhpWord/PhpWord.php
+++ b/src/PhpWord/PhpWord.php
@@ -22,24 +22,22 @@ use PhpOffice\PhpWord\Collection\Footnotes;
use PhpOffice\PhpWord\Collection\Titles;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
-use PhpOffice\PhpWord\Style;
/**
* PHPWord main class
*/
class PhpWord
{
- const DEFAULT_FONT_COLOR = '000000'; // HEX
- const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
- const DEFAULT_FONT_NAME = 'Arial';
-
/**
- * Default font size, in points.
+ * Default font settings
*
- * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
- * use, and the conversion will be conducted during XML writing.
+ * @const string|int
+ * @deprecated 0.11.0 Use Settings constants
*/
- const DEFAULT_FONT_SIZE = 10;
+ const DEFAULT_FONT_NAME = Settings::DEFAULT_FONT_NAME;
+ const DEFAULT_FONT_SIZE = Settings::DEFAULT_FONT_SIZE;
+ const DEFAULT_FONT_COLOR = Settings::DEFAULT_FONT_COLOR;
+ const DEFAULT_FONT_CONTENT_TYPE = Settings::DEFAULT_FONT_CONTENT_TYPE;
/**
* Document properties object
@@ -76,19 +74,6 @@ class PhpWord
*/
private $endnotes;
- /**
- * Default font name
- *
- * @var string
- */
- private $defaultFontName;
-
- /**
- * Default font size
- * @var int
- */
- private $defaultFontSize;
-
/**
* Create new
*/
@@ -98,8 +83,6 @@ class PhpWord
$this->titles = new Titles();
$this->footnotes = new Footnotes();
$this->endnotes = new Endnotes();
- $this->defaultFontName = self::DEFAULT_FONT_NAME;
- $this->defaultFontSize = self::DEFAULT_FONT_SIZE;
}
/**
@@ -220,7 +203,7 @@ class PhpWord
*/
public function getDefaultFontName()
{
- return $this->defaultFontName;
+ return Settings::getDefaultFontName();
}
/**
@@ -230,7 +213,7 @@ class PhpWord
*/
public function setDefaultFontName($fontName)
{
- $this->defaultFontName = $fontName;
+ Settings::setDefaultFontName($fontName);
}
/**
@@ -240,7 +223,7 @@ class PhpWord
*/
public function getDefaultFontSize()
{
- return $this->defaultFontSize;
+ return Settings::getDefaultFontSize();
}
/**
@@ -250,7 +233,7 @@ class PhpWord
*/
public function setDefaultFontSize($fontSize)
{
- $this->defaultFontSize = $fontSize;
+ Settings::setDefaultFontSize($fontSize);
}
/**
diff --git a/src/PhpWord/Reader/ODText/AbstractPart.php b/src/PhpWord/Reader/ODText/AbstractPart.php
index 7a91e12d..688ad9eb 100644
--- a/src/PhpWord/Reader/ODText/AbstractPart.php
+++ b/src/PhpWord/Reader/ODText/AbstractPart.php
@@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\ODText;
-use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Reader\Word2007\AbstractPart as Word2007AbstractPart;
+use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Abstract part reader
diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php
index ebd79cd9..17bc3429 100644
--- a/src/PhpWord/Settings.php
+++ b/src/PhpWord/Settings.php
@@ -48,14 +48,25 @@ class Settings
* - Indentation: left, right, firstLine, hanging
* - Spacing: before, after
*
- * @const int|float
+ * @const string
*/
- const UNIT_TWIP = 1; // = 1/20 point
- const UNIT_CM = 567;
- const UNIT_MM = 56.7;
- const UNIT_INCH = 1440;
- const UNIT_POINT = 20; // = 1/72 inch
- const UNIT_PICA = 240; // = 1/6 inch = 12 points
+ const UNIT_TWIP = 'twip'; // = 1/20 point
+ const UNIT_CM = 'cm';
+ const UNIT_MM = 'mm';
+ const UNIT_INCH = 'inch';
+ const UNIT_POINT = 'point'; // = 1/72 inch
+ const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
+
+ /**
+ * Default font settings
+ *
+ * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
+ * use, and the conversion will be conducted during XML writing.
+ */
+ const DEFAULT_FONT_NAME = 'Arial';
+ const DEFAULT_FONT_SIZE = 10;
+ const DEFAULT_FONT_COLOR = '000000';
+ const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
/**
* Compatibility option for XMLWriter
@@ -71,13 +82,6 @@ class Settings
*/
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
*
@@ -99,6 +103,19 @@ class Settings
*/
private static $measurementUnit = self::UNIT_TWIP;
+ /**
+ * Default font name
+ *
+ * @var string
+ */
+ private static $defaultFontName = self::DEFAULT_FONT_NAME;
+
+ /**
+ * Default font size
+ * @var int
+ */
+ private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
+
/**
* Return the compatibility option used by the XMLWriter
*
@@ -119,12 +136,10 @@ class Settings
*/
public static function setCompatibility($compatibility)
{
- if (is_bool($compatibility)) {
- self::$xmlWriterCompatibility = $compatibility;
- return true;
- }
+ $compatibility = (bool)$compatibility;
+ self::$xmlWriterCompatibility = $compatibility;
- return false;
+ return true;
}
/**
@@ -145,8 +160,7 @@ class Settings
*/
public static function setZipClass($zipClass)
{
- if (($zipClass === self::PCLZIP) ||
- ($zipClass === self::ZIPARCHIVE)) {
+ if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE))) {
self::$zipClass = $zipClass;
return true;
}
@@ -186,7 +200,8 @@ class Settings
*/
public static function setPdfRendererName($libraryName)
{
- if (!in_array($libraryName, self::$pdfRenderers)) {
+ $pdfRenderers = array(self::PDF_RENDERER_DOMPDF);
+ if (!in_array($libraryName, $pdfRenderers)) {
return false;
}
self::$pdfRendererName = $libraryName;
@@ -222,7 +237,7 @@ class Settings
/**
* Get measurement unit
*
- * @return int|float
+ * @return string
*/
public static function getMeasurementUnit()
{
@@ -232,7 +247,7 @@ class Settings
/**
* Set measurement unit
*
- * @param int|float $value
+ * @param string $value
* @return bool
*/
public static function setMeasurementUnit($value)
@@ -247,6 +262,102 @@ class Settings
return true;
}
+ /**
+ * Get default font name
+ *
+ * @return string
+ */
+ public static function getDefaultFontName()
+ {
+ return self::$defaultFontName;
+ }
+
+ /**
+ * Set default font name
+ *
+ * @param string $value
+ * @return bool
+ */
+ public static function setDefaultFontName($value)
+ {
+ if (is_string($value) && trim($value) !== '') {
+ self::$defaultFontName = $value;
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Get default font size
+ *
+ * @return integer
+ */
+ public static function getDefaultFontSize()
+ {
+ return self::$defaultFontSize;
+ }
+
+ /**
+ * Set default font size
+ *
+ * @param int $value
+ * @return bool
+ */
+ public static function setDefaultFontSize($value)
+ {
+ $value = intval($value);
+ if ($value > 0) {
+ self::$defaultFontSize = $value;
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Load setting from phpword.yml or phpword.yml.dist
+ *
+ * @param string $filename
+ * @return array
+ */
+ public static function loadConfig($filename = null)
+ {
+ // Get config file
+ $configFile = null;
+ $configPath = __DIR__ . '/../../';
+ if ($filename !== null) {
+ $files = array($filename);
+ } else {
+ $files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist");
+ }
+ foreach ($files as $file) {
+ if (file_exists($file)) {
+ $configFile = realpath($file);
+ break;
+ }
+ }
+
+ // Parse config file
+ $config = array();
+ if ($configFile !== null) {
+ $config = parse_ini_file($configFile);
+ if ($config === false) {
+ return $config;
+ }
+ }
+
+ // Set config value
+ foreach ($config as $key => $value) {
+ $method = "set{$key}";
+ if (method_exists(__CLASS__, $method)) {
+ self::$method($value);
+ }
+ }
+
+ return $config;
+ }
+
/**
* Return the compatibility option used by the XMLWriter
*
diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php
index fd1f1f60..fdc6e29d 100644
--- a/src/PhpWord/Shared/XMLWriter.php
+++ b/src/PhpWord/Shared/XMLWriter.php
@@ -28,12 +28,13 @@ if (!defined('DATE_W3C')) {
/**
* XMLWriter wrapper
*
- * @method bool writeElement(string $name, string $content = null)
- * @method bool startElement(string $name)
- * @method bool writeAttribute(string $name, string $value)
* @method bool endElement()
* @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
+ * @method bool startElement(string $name)
* @method bool text(string $content)
+ * @method bool writeAttribute(string $name, string $value)
+ * @method bool writeElement(string $name, string $content = null)
+ * @method bool writeRaw(string $content)
*/
class XMLWriter
{
@@ -135,21 +136,6 @@ class XMLWriter
}
}
- /**
- * Fallback method for writeRaw, introduced in PHP 5.2
- *
- * @param string $text
- * @return bool
- */
- public function writeRaw($text)
- {
- if (isset($this->xmlWriter) && is_object($this->xmlWriter) && (method_exists($this->xmlWriter, 'writeRaw'))) {
- return $this->xmlWriter->writeRaw($text);
- }
-
- return $this->text($text);
- }
-
/**
* Write element if ...
*
diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php
index 5440f512..07eebdb2 100644
--- a/src/PhpWord/Style/Font.php
+++ b/src/PhpWord/Style/Font.php
@@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Style;
-use PhpOffice\PhpWord\PhpWord;
-
/**
* Font style
*/
@@ -86,30 +84,30 @@ class Font extends AbstractStyle
/**
* Font name
*
- * @var int|float
+ * @var string
*/
- private $name = PhpWord::DEFAULT_FONT_NAME;
+ private $name;
/**
* Font Content Type
*
* @var string
*/
- private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE;
+ private $hint;
/**
* Font size
*
* @var int|float
*/
- private $size = PhpWord::DEFAULT_FONT_SIZE;
+ private $size;
/**
* Font color
*
* @var string
*/
- private $color = PhpWord::DEFAULT_FONT_COLOR;
+ private $color;
/**
* Bold
@@ -241,9 +239,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
- public function setName($value = PhpWord::DEFAULT_FONT_NAME)
+ public function setName($value = null)
{
- $this->name = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_NAME);
+ $this->name = $value;
return $this;
}
@@ -264,9 +262,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
- public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE)
+ public function setHint($value = null)
{
- $this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE);
+ $this->hint = $value;
return $this;
}
@@ -287,9 +285,9 @@ class Font extends AbstractStyle
* @param int|float $value
* @return self
*/
- public function setSize($value = PhpWord::DEFAULT_FONT_SIZE)
+ public function setSize($value = null)
{
- $this->size = $this->setNumericVal($value, PhpWord::DEFAULT_FONT_SIZE);
+ $this->size = $this->setNumericVal($value, $this->size);
return $this;
}
@@ -310,9 +308,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
- public function setColor($value = PhpWord::DEFAULT_FONT_COLOR)
+ public function setColor($value = null)
{
- $this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR);
+ $this->color = $value;
return $this;
}
diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php
index 90e8282f..68b53463 100644
--- a/src/PhpWord/Style/Table.php
+++ b/src/PhpWord/Style/Table.php
@@ -430,7 +430,7 @@ class Table extends Border
/**
* Get cell margin
*
- * @return int[]
+ * @return integer[]
*/
public function getCellMargin()
{
diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php
index db1723aa..73c6474b 100644
--- a/src/PhpWord/Writer/HTML.php
+++ b/src/PhpWord/Writer/HTML.php
@@ -19,9 +19,10 @@ namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
-use PhpOffice\PhpWord\Style;
+use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
+use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\HTML\Element\Container;
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
@@ -168,14 +169,13 @@ class HTML extends AbstractWriter implements WriterInterface
*/
private function writeStyles()
{
- $phpWord = $this->getPhpWord();
$css = '