Skip to content

Commit 5103732

Browse files
author
boshi_no_1
committed
为PHPWord的MPDF插件添加config参数,使其可以正常被使用。
1 parent 2daa50c commit 5103732

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

src/PhpWord/IOFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ abstract class IOFactory
3333
*
3434
* @return WriterInterface
3535
*/
36-
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
36+
public static function createWriter(PhpWord $phpWord, $name = 'Word2007', $config = [])
3737
{
3838
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
3939
throw new Exception("\"{$name}\" is not a valid writer.");
4040
}
4141

4242
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
43-
44-
return new $fqName($phpWord);
43+
return new $fqName($phpWord, $config);
4544
}
4645

4746
/**

src/PhpWord/Writer/PDF.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PDF
3939
/**
4040
* Instantiate a new renderer of the configured type within this container class.
4141
*/
42-
public function __construct(PhpWord $phpWord)
42+
public function __construct(PhpWord $phpWord, $config = [])
4343
{
4444
$pdfLibraryName = Settings::getPdfRendererName();
4545
$pdfLibraryPath = Settings::getPdfRendererPath();
@@ -54,7 +54,7 @@ public function __construct(PhpWord $phpWord)
5454
}
5555

5656
$rendererName = static::class . '\\' . $pdfLibraryName;
57-
$this->renderer = new $rendererName($phpWord);
57+
$this->renderer = new $rendererName($phpWord, $config);
5858
}
5959

6060
/**

src/PhpWord/Writer/PDF/AbstractRenderer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ abstract class AbstractRenderer extends HTML
7373
9 => 'A4', // (210 mm by 297 mm)
7474
];
7575

76+
protected $config;
77+
7678
/**
7779
* Create new instance.
7880
*
7981
* @param PhpWord $phpWord PhpWord object
8082
*/
81-
public function __construct(PhpWord $phpWord)
83+
public function __construct(PhpWord $phpWord, $config = [])
8284
{
8385
parent::__construct($phpWord);
86+
$this->config = $config;
8487
$this->isPdf = true;
8588
if ($this->includeFile != null) {
8689
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;

src/PhpWord/Writer/PDF/MPDF.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class MPDF extends AbstractRenderer implements WriterInterface
3737
*
3838
* @codeCoverageIgnore
3939
*/
40-
public function __construct(PhpWord $phpWord)
40+
public function __construct(PhpWord $phpWord, $config = [])
4141
{
4242
if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) {
4343
// MPDF version 5.* needs this file to be included, later versions not
4444
$this->includeFile = 'mpdf.php';
4545
}
46-
parent::__construct($phpWord);
46+
parent::__construct($phpWord, $config);
4747
}
4848

4949
/**
@@ -55,7 +55,7 @@ protected function createExternalWriterInstance()
5555
{
5656
$mPdfClass = $this->getMPdfClassName();
5757

58-
$options = [];
58+
$options = $this->config;
5959
if ($this->getFont()) {
6060
$options['default_font'] = $this->getFont();
6161
}

tests/PhpWordTests/Writer/PDF/MPDFTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testConstruct(): void
4646
$section = $phpWord->addSection($oSettings); // @phpstan-ignore-line
4747
$section->addText('Section 2 - landscape');
4848

49-
$writer = new MPDF($phpWord);
49+
$writer = new MPDF($phpWord, ['mode' => 'zh-cn', 'margin_top' => 28]);
5050
$writer->save($file);
5151

5252
self::assertFileExists($file);

0 commit comments

Comments
 (0)