Skip to content

Commit

Permalink
Basic PDF Writer PHPOffice#68
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed Apr 13, 2014
1 parent d54d47d commit 580a61a
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
26 changes: 19 additions & 7 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
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;
Expand All @@ -22,9 +32,6 @@
$pageTitle .= 'PHPWord';
$pageHeading = IS_INDEX ? '' : "<h1>{$pageHeading}</h1>";

// Set writers
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html');

// Populate samples
$files = '';
if ($handle = opendir('.')) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
98 changes: 93 additions & 5 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
*/
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
*
Expand All @@ -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
*
Expand Down Expand Up @@ -74,7 +96,7 @@ public static function setZipClass($zipClass)
return true;
}
return false;
} // function setZipClass()
}

/**
* Return the name of the Zip handler Class that PHPWord is configured to use (PCLZip or ZipArchive)
Expand All @@ -87,5 +109,71 @@ public static function setZipClass($zipClass)
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;
}
}
36 changes: 22 additions & 14 deletions src/PhpWord/Writer/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function save($filename = null)
*
* @return string
*/
private function writeDocument()
public function writeDocument()
{
$html = '';
$html .= '<!DOCTYPE html>' . PHP_EOL;
Expand All @@ -87,7 +88,7 @@ private function writeDocument()
*
* @return string
*/
public function writeHTMLHead()
private function writeHTMLHead()
{
$properties = $this->getPhpWord()->getDocumentProperties();
$propertiesMapping = array(
Expand Down Expand Up @@ -124,7 +125,7 @@ public function writeHTMLHead()
*
* @return string
*/
public function writeHTMLBody()
private function writeHTMLBody()
{
$phpWord = $this->getPhpWord();
$html = '';
Expand All @@ -136,8 +137,8 @@ public function writeHTMLBody()
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) {
Expand All @@ -161,9 +162,9 @@ public function writeHTMLBody()
} 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);
}
}
}
Expand Down Expand Up @@ -248,9 +249,9 @@ private function writeTextRun($textrun)
} 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 .= '</p>' . PHP_EOL;
Expand All @@ -263,6 +264,7 @@ private function writeTextRun($textrun)
* Write link
*
* @param Link $element
* @param boolean $withoutP
* @return string
*/
private function writeLink($element, $withoutP = false)
Expand Down Expand Up @@ -300,6 +302,7 @@ private function writeTitle($element)
* Write preserve text
*
* @param PreserveText $element
* @param boolean $withoutP
* @return string
*/
private function writePreserveText($element, $withoutP = false)
Expand Down Expand Up @@ -350,7 +353,7 @@ private function writeListItem($element)
/**
* Write table
*
* @param Title $element
* @param Table $element
* @return string
*/
private function writeTable($element)
Expand All @@ -361,7 +364,7 @@ private function writeTable($element)
if ($cRows > 0) {
$html .= "<table>" . PHP_EOL;
foreach ($rows as $row) {
$height = $row->getHeight();
// $height = $row->getHeight();
$rowStyle = $row->getStyle();
$tblHeader = $rowStyle->getTblHeader();
$html .= "<tr>" . PHP_EOL;
Expand All @@ -388,13 +391,13 @@ private function writeTable($element)
} 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 .= "</td>" . PHP_EOL;
}
Expand All @@ -410,6 +413,7 @@ private function writeTable($element)
* Write image
*
* @param Image $element
* @param boolean $withoutP
* @return string
*/
private function writeImage($element, $withoutP = false)
Expand All @@ -421,6 +425,7 @@ private function writeImage($element, $withoutP = false)
* Write object
*
* @param Object $element
* @param boolean $withoutP
* @return string
*/
private function writeObject($element, $withoutP = false)
Expand Down Expand Up @@ -478,11 +483,14 @@ private function writeUnsupportedElement($element, $withoutP = false)
*/
private function writeStyles()
{
$bodyCss = array();
$css = '<style>' . PHP_EOL;

// Default styles
$bodyCss['font-family'] = "'" . $this->getPhpWord()->getDefaultFontName() . "'";
$bodyCss['font-size'] = $this->getPhpWord()->getDefaultFontSize() . 'pt';
$css .= '* ' . $this->assembleCss($bodyCss, true) . PHP_EOL;

// Custom styles
$styles = Style::getStyles();
if (is_array($styles)) {
Expand Down
Loading

0 comments on commit 580a61a

Please sign in to comment.