Skip to content

Commit f7bee23

Browse files
committed
Add TCPDF and mPDF as optional PDF renderer library
1 parent 553371f commit f7bee23

File tree

7 files changed

+178
-30
lines changed

7 files changed

+178
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
2323
- ODT: Enable inline font style in TextRun - @ivanlanin
2424
- ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
2525
- ODT: Enable section and column - @ivanlanin
26+
- PDF: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
2627

2728
### Bugfixes
2829

src/PhpWord/Settings.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Settings
3939
* @const string
4040
*/
4141
const PDF_RENDERER_DOMPDF = 'DomPDF';
42+
const PDF_RENDERER_TCPDF = 'TCPDF';
43+
const PDF_RENDERER_MPDF = 'MPDF';
4244

4345
/**
4446
* Measurement units multiplication factor
@@ -201,7 +203,7 @@ public static function getPdfRendererName()
201203
*/
202204
public static function setPdfRendererName($libraryName)
203205
{
204-
$pdfRenderers = array(self::PDF_RENDERER_DOMPDF);
206+
$pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF);
205207
if (!in_array($libraryName, $pdfRenderers)) {
206208
return false;
207209
}

src/PhpWord/Writer/PDF.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
/**
2525
* PDF Writer
26+
*
27+
* @since 0.10.0
2628
*/
2729
class PDF
2830
{

src/PhpWord/Writer/PDF/AbstractRenderer.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@
1919

2020
use PhpOffice\PhpWord\Exception\Exception;
2121
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Settings;
2223
use PhpOffice\PhpWord\Writer\HTML;
2324

2425
/**
2526
* Abstract PDF renderer
27+
*
28+
* @since 0.10.0
2629
*/
2730
abstract class AbstractRenderer extends HTML
2831
{
32+
/**
33+
* Name of renderer include file
34+
*
35+
* @var string
36+
*/
37+
protected $includeFile;
38+
2939
/**
3040
* Temporary storage directory
3141
*
@@ -45,14 +55,14 @@ abstract class AbstractRenderer extends HTML
4555
*
4656
* @var int
4757
*/
48-
protected $paperSize = null;
58+
protected $paperSize;
4959

5060
/**
5161
* Orientation
5262
*
5363
* @var string
5464
*/
55-
protected $orientation = null;
65+
protected $orientation;
5666

5767
/**
5868
* Paper Sizes xRef List
@@ -67,10 +77,17 @@ abstract class AbstractRenderer extends HTML
6777
* Create new instance
6878
*
6979
* @param PhpWord $phpWord PhpWord object
80+
* @throws \PhpOffice\PhpWord\Exception\Exception
7081
*/
7182
public function __construct(PhpWord $phpWord)
7283
{
7384
parent::__construct($phpWord);
85+
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
86+
if (file_exists($includeFile)) {
87+
require_once $includeFile;
88+
} else {
89+
throw new Exception('Unable to load PDF Rendering library');
90+
}
7491
}
7592

7693
/**

src/PhpWord/Writer/PDF/DomPDF.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,39 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\PDF;
1919

20-
use PhpOffice\PhpWord\Exception\Exception;
21-
use PhpOffice\PhpWord\PhpWord;
22-
use PhpOffice\PhpWord\Settings;
2320
use PhpOffice\PhpWord\Writer\WriterInterface;
2421

2522
/**
2623
* DomPDF writer
24+
*
25+
* @link https://github.com/dompdf/dompdf
26+
* @since 0.10.0
2727
*/
2828
class DomPDF extends AbstractRenderer implements WriterInterface
2929
{
3030
/**
31-
* Create new instance
31+
* Name of renderer include file
3232
*
33-
* @param PhpWord $phpWord PhpWord object
34-
* @throws \PhpOffice\PhpWord\Exception\Exception
33+
* @var string
3534
*/
36-
public function __construct(PhpWord $phpWord)
37-
{
38-
parent::__construct($phpWord);
39-
$configFile = Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
40-
if (file_exists($configFile)) {
41-
require_once $configFile;
42-
} else {
43-
throw new Exception('Unable to load PDF Rendering library');
44-
}
45-
}
35+
protected $includeFile = 'dompdf_config.inc.php';
4636

4737
/**
4838
* Save PhpWord to file
4939
*
5040
* @param string $filename Name of the file to save as
51-
* @throws Exception
5241
*/
5342
public function save($filename = null)
5443
{
5544
$fileHandle = parent::prepareForSave($filename);
5645

57-
// Default PDF paper size
46+
// PDF settings
5847
$paperSize = 'A4';
59-
$orientation = 'P';
60-
$printPaperSize = 9;
61-
62-
if (isset(self::$paperSizes[$printPaperSize])) {
63-
$paperSize = self::$paperSizes[$printPaperSize];
64-
}
65-
66-
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
48+
$orientation = 'portrait';
6749

6850
// Create PDF
6951
$pdf = new \DOMPDF();
7052
$pdf->set_paper(strtolower($paperSize), $orientation);
71-
7253
$pdf->load_html($this->writeDocument());
7354
$pdf->render();
7455

src/PhpWord/Writer/PDF/MPDF.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PhpWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\PDF;
19+
20+
use PhpOffice\PhpWord\Writer\WriterInterface;
21+
22+
/**
23+
* MPDF writer
24+
*
25+
* @link http://www.mpdf1.com/
26+
* @since 0.11.0
27+
*/
28+
class MPDF extends AbstractRenderer implements WriterInterface
29+
{
30+
/**
31+
* Name of renderer include file
32+
*
33+
* @var string
34+
*/
35+
protected $includeFile = 'mdf.php';
36+
37+
/**
38+
* Save PhpWord to file
39+
*
40+
* @param string $filename Name of the file to save as
41+
*/
42+
public function save($filename = null)
43+
{
44+
$fileHandle = parent::prepareForSave($filename);
45+
46+
// PDF settings
47+
$paperSize = strtoupper('A4');
48+
$orientation = strtoupper('portrait');
49+
50+
// Create PDF
51+
$pdf = new \mpdf();
52+
$pdf->_setPageSize($paperSize, $orientation);
53+
$pdf->defOrientation = $orientation;
54+
$pdf->addPage($orientation);
55+
56+
// Write document properties
57+
$phpWord = $this->getPhpWord();
58+
$docProps = $phpWord->getDocumentProperties();
59+
$pdf->setTitle($docProps->getTitle());
60+
$pdf->setAuthor($docProps->getCreator());
61+
$pdf->setSubject($docProps->getSubject());
62+
$pdf->setKeywords($docProps->getKeywords());
63+
$pdf->setCreator($docProps->getCreator());
64+
65+
$pdf->writeHTML($this->writeDocument());
66+
67+
// Write to file
68+
fwrite($fileHandle, $pdf->output($filename, 'S'));
69+
70+
parent::restoreStateAfterSave($fileHandle);
71+
}
72+
}

src/PhpWord/Writer/PDF/TCPDF.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PhpWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\PDF;
19+
20+
use PhpOffice\PhpWord\Writer\WriterInterface;
21+
22+
/**
23+
* TCPDF writer
24+
*
25+
* @link http://www.tcpdf.org/
26+
* @since 0.11.0
27+
*/
28+
class TCPDF extends AbstractRenderer implements WriterInterface
29+
{
30+
/**
31+
* Name of renderer include file
32+
*
33+
* @var string
34+
*/
35+
protected $includeFile = 'tcpdf.php';
36+
37+
/**
38+
* Save PhpWord to file
39+
*
40+
* @param string $filename Name of the file to save as
41+
*/
42+
public function save($filename = null)
43+
{
44+
$fileHandle = parent::prepareForSave($filename);
45+
46+
// PDF settings
47+
$paperSize = 'A4';
48+
$orientation = 'P';
49+
50+
// Create PDF
51+
$pdf = new \TCPDF($orientation, 'pt', $paperSize);
52+
$pdf->setFontSubsetting(false);
53+
$pdf->setPrintHeader(false);
54+
$pdf->setPrintFooter(false);
55+
$pdf->addPage();
56+
$pdf->setFont($this->getFont());
57+
$pdf->writeHTML($this->writeDocument());
58+
59+
// Write document properties
60+
$phpWord = $this->getPhpWord();
61+
$docProps = $phpWord->getDocumentProperties();
62+
$pdf->setTitle($docProps->getTitle());
63+
$pdf->setAuthor($docProps->getCreator());
64+
$pdf->setSubject($docProps->getSubject());
65+
$pdf->setKeywords($docProps->getKeywords());
66+
$pdf->setCreator($docProps->getCreator());
67+
68+
// Write to file
69+
fwrite($fileHandle, $pdf->output($filename, 'S'));
70+
71+
parent::restoreStateAfterSave($fileHandle);
72+
}
73+
}

0 commit comments

Comments
 (0)