diff --git a/CHANGELOG.md b/CHANGELOG.md index bc42fc4a..cb408f43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 7336d6bd..8d9e2ace 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -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; } diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index fc4599c9..98dc1220 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -23,6 +23,8 @@ use PhpOffice\PhpWord\Settings; /** * PDF Writer + * + * @since 0.10.0 */ class PDF { diff --git a/src/PhpWord/Writer/PDF/AbstractRenderer.php b/src/PhpWord/Writer/PDF/AbstractRenderer.php index 71383329..f7266d1a 100644 --- a/src/PhpWord/Writer/PDF/AbstractRenderer.php +++ b/src/PhpWord/Writer/PDF/AbstractRenderer.php @@ -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'); + } } /** diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index df599e7b..4effc154 100644 --- a/src/PhpWord/Writer/PDF/DomPDF.php +++ b/src/PhpWord/Writer/PDF/DomPDF.php @@ -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(); diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php new file mode 100644 index 00000000..77bbafd6 --- /dev/null +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -0,0 +1,72 @@ +_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); + } +} diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php new file mode 100644 index 00000000..05f02756 --- /dev/null +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -0,0 +1,73 @@ +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); + } +}