Add TCPDF and mPDF as optional PDF renderer library

This commit is contained in:
Ivan Lanin 2014-05-19 17:39:31 +07:00
parent 553371f088
commit f7bee23f65
7 changed files with 178 additions and 30 deletions

View File

@ -23,6 +23,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- ODT: Enable inline font style in TextRun - @ivanlanin
- ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
- ODT: Enable section and column - @ivanlanin
- PDF: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
### Bugfixes

View File

@ -39,6 +39,8 @@ class Settings
* @const string
*/
const PDF_RENDERER_DOMPDF = 'DomPDF';
const PDF_RENDERER_TCPDF = 'TCPDF';
const PDF_RENDERER_MPDF = 'MPDF';
/**
* Measurement units multiplication factor
@ -201,7 +203,7 @@ class Settings
*/
public static function setPdfRendererName($libraryName)
{
$pdfRenderers = array(self::PDF_RENDERER_DOMPDF);
$pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF);
if (!in_array($libraryName, $pdfRenderers)) {
return false;
}

View File

@ -23,6 +23,8 @@ use PhpOffice\PhpWord\Settings;
/**
* PDF Writer
*
* @since 0.10.0
*/
class PDF
{

View File

@ -19,13 +19,23 @@ namespace PhpOffice\PhpWord\Writer\PDF;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Writer\HTML;
/**
* Abstract PDF renderer
*
* @since 0.10.0
*/
abstract class AbstractRenderer extends HTML
{
/**
* Name of renderer include file
*
* @var string
*/
protected $includeFile;
/**
* Temporary storage directory
*
@ -45,14 +55,14 @@ abstract class AbstractRenderer extends HTML
*
* @var int
*/
protected $paperSize = null;
protected $paperSize;
/**
* Orientation
*
* @var string
*/
protected $orientation = null;
protected $orientation;
/**
* Paper Sizes xRef List
@ -67,10 +77,17 @@ abstract class AbstractRenderer extends HTML
* Create new instance
*
* @param PhpWord $phpWord PhpWord object
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function __construct(PhpWord $phpWord)
{
parent::__construct($phpWord);
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
if (file_exists($includeFile)) {
require_once $includeFile;
} else {
throw new Exception('Unable to load PDF Rendering library');
}
}
/**

View File

@ -17,58 +17,39 @@
namespace PhpOffice\PhpWord\Writer\PDF;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Writer\WriterInterface;
/**
* DomPDF writer
*
* @link https://github.com/dompdf/dompdf
* @since 0.10.0
*/
class DomPDF extends AbstractRenderer implements WriterInterface
{
/**
* Create new instance
* Name of renderer include file
*
* @param PhpWord $phpWord PhpWord object
* @throws \PhpOffice\PhpWord\Exception\Exception
* @var string
*/
public function __construct(PhpWord $phpWord)
{
parent::__construct($phpWord);
$configFile = Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
if (file_exists($configFile)) {
require_once $configFile;
} else {
throw new Exception('Unable to load PDF Rendering library');
}
}
protected $includeFile = 'dompdf_config.inc.php';
/**
* Save PhpWord to file
*
* @param string $filename Name of the file to save as
* @throws Exception
*/
public function save($filename = null)
{
$fileHandle = parent::prepareForSave($filename);
// Default PDF paper size
// PDF settings
$paperSize = 'A4';
$orientation = 'P';
$printPaperSize = 9;
if (isset(self::$paperSizes[$printPaperSize])) {
$paperSize = self::$paperSizes[$printPaperSize];
}
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
$orientation = 'portrait';
// Create PDF
$pdf = new \DOMPDF();
$pdf->set_paper(strtolower($paperSize), $orientation);
$pdf->load_html($this->writeDocument());
$pdf->render();

View File

@ -0,0 +1,72 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PhpWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\PDF;
use PhpOffice\PhpWord\Writer\WriterInterface;
/**
* MPDF writer
*
* @link http://www.mpdf1.com/
* @since 0.11.0
*/
class MPDF extends AbstractRenderer implements WriterInterface
{
/**
* Name of renderer include file
*
* @var string
*/
protected $includeFile = 'mdf.php';
/**
* Save PhpWord to file
*
* @param string $filename Name of the file to save as
*/
public function save($filename = null)
{
$fileHandle = parent::prepareForSave($filename);
// PDF settings
$paperSize = strtoupper('A4');
$orientation = strtoupper('portrait');
// Create PDF
$pdf = new \mpdf();
$pdf->_setPageSize($paperSize, $orientation);
$pdf->defOrientation = $orientation;
$pdf->addPage($orientation);
// Write document properties
$phpWord = $this->getPhpWord();
$docProps = $phpWord->getDocumentProperties();
$pdf->setTitle($docProps->getTitle());
$pdf->setAuthor($docProps->getCreator());
$pdf->setSubject($docProps->getSubject());
$pdf->setKeywords($docProps->getKeywords());
$pdf->setCreator($docProps->getCreator());
$pdf->writeHTML($this->writeDocument());
// Write to file
fwrite($fileHandle, $pdf->output($filename, 'S'));
parent::restoreStateAfterSave($fileHandle);
}
}

View File

@ -0,0 +1,73 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PhpWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\PDF;
use PhpOffice\PhpWord\Writer\WriterInterface;
/**
* TCPDF writer
*
* @link http://www.tcpdf.org/
* @since 0.11.0
*/
class TCPDF extends AbstractRenderer implements WriterInterface
{
/**
* Name of renderer include file
*
* @var string
*/
protected $includeFile = 'tcpdf.php';
/**
* Save PhpWord to file
*
* @param string $filename Name of the file to save as
*/
public function save($filename = null)
{
$fileHandle = parent::prepareForSave($filename);
// PDF settings
$paperSize = 'A4';
$orientation = 'P';
// Create PDF
$pdf = new \TCPDF($orientation, 'pt', $paperSize);
$pdf->setFontSubsetting(false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->addPage();
$pdf->setFont($this->getFont());
$pdf->writeHTML($this->writeDocument());
// Write document properties
$phpWord = $this->getPhpWord();
$docProps = $phpWord->getDocumentProperties();
$pdf->setTitle($docProps->getTitle());
$pdf->setAuthor($docProps->getCreator());
$pdf->setSubject($docProps->getSubject());
$pdf->setKeywords($docProps->getKeywords());
$pdf->setCreator($docProps->getCreator());
// Write to file
fwrite($fileHandle, $pdf->output($filename, 'S'));
parent::restoreStateAfterSave($fileHandle);
}
}