Skip to content

为PHPWord的MPDF插件添加config参数,使其可以正常被使用。 #2612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
7 changes: 3 additions & 4 deletions src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/PhpWord/IOFactory.php

GitHub Actions / phpstan

Ignored error pattern #^Method PhpOffice\\PhpWord\\IOFactory\:\:createWriter\(\) should return PhpOffice\\PhpWord\\Writer\\WriterInterface but returns PhpOffice\\PhpWord\\Writer\\PDF\|PhpOffice\\PhpWord\\Writer\\WriterInterface\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/IOFactory.php was not matched in reported errors.
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
@@ -30,18 +30,17 @@
* Create new writer.
*
* @param string $name
*
* @return WriterInterface
* @param array $config
*/
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
public static function createWriter(PhpWord $phpWord, $name = 'Word2007', $config = []): WriterInterface
{
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
throw new Exception("\"{$name}\" is not a valid writer.");
}

$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";

return new $fqName($phpWord);
return new $fqName($phpWord, $config);
}

/**
18 changes: 18 additions & 0 deletions src/PhpWord/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
@@ -93,6 +93,24 @@ abstract class AbstractWriter implements WriterInterface
*/
private $tempFilename;

/**
* some options in config.
*
* @var array
*/
protected $config;

/**
* construct method.
*
* @param array $config
*/
public function __construct(?PhpWord $phpWord = null, $config = [])
{
$this->setPhpWord($phpWord);
$this->config = $config;
}

/**
* Get PhpWord object.
*
7 changes: 5 additions & 2 deletions src/PhpWord/Writer/HTML.php
Original file line number Diff line number Diff line change
@@ -68,10 +68,13 @@ class HTML extends AbstractWriter implements WriterInterface

/**
* Create new instance.
*
* @param array $config
*/
public function __construct(?PhpWord $phpWord = null)
public function __construct(?PhpWord $phpWord = null, $config = [])
{
$this->setPhpWord($phpWord);
// Assign PhpWord
parent::__construct($phpWord, $config);

$this->parts = ['Head', 'Body'];
foreach ($this->parts as $partName) {
6 changes: 4 additions & 2 deletions src/PhpWord/Writer/ODText.php
Original file line number Diff line number Diff line change
@@ -38,11 +38,13 @@ class ODText extends AbstractWriter implements WriterInterface

/**
* Create new ODText writer.
*
* @param array $config
*/
public function __construct(?PhpWord $phpWord = null)
public function __construct(?PhpWord $phpWord = null, $config = [])
{
// Assign PhpWord
$this->setPhpWord($phpWord);
parent::__construct($phpWord, $config);

// Create parts
$this->parts = [
8 changes: 5 additions & 3 deletions src/PhpWord/Writer/PDF.php
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
*
* @since 0.10.0
*/
class PDF
class PDF implements WriterInterface
{
/**
* The wrapper for the requested PDF rendering engine.
@@ -38,8 +38,10 @@ class PDF

/**
* Instantiate a new renderer of the configured type within this container class.
*
* @param array $config
*/
public function __construct(PhpWord $phpWord)
public function __construct(PhpWord $phpWord, $config = [])
{
$pdfLibraryName = Settings::getPdfRendererName();
$pdfLibraryPath = Settings::getPdfRendererPath();
@@ -54,7 +56,7 @@ public function __construct(PhpWord $phpWord)
}

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

/**
8 changes: 5 additions & 3 deletions src/PhpWord/Writer/PDF/AbstractRenderer.php
Original file line number Diff line number Diff line change
@@ -76,11 +76,13 @@ abstract class AbstractRenderer extends HTML
/**
* Create new instance.
*
* @param PhpWord $phpWord PhpWord object
* @param array $config
*/
public function __construct(PhpWord $phpWord)
public function __construct(PhpWord $phpWord, $config = [])
{
parent::__construct($phpWord);
// Assign PhpWord
parent::__construct($phpWord, $config);

$this->isPdf = true;
if ($this->includeFile != null) {
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
7 changes: 4 additions & 3 deletions src/PhpWord/Writer/PDF/MPDF.php
Original file line number Diff line number Diff line change
@@ -35,15 +35,16 @@ class MPDF extends AbstractRenderer implements WriterInterface
/**
* Overridden to set the correct includefile, only needed for MPDF 5.
*
* @param array $config
* @codeCoverageIgnore
*/
public function __construct(PhpWord $phpWord)
public function __construct(PhpWord $phpWord, $config = [])
{
if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) {
// MPDF version 5.* needs this file to be included, later versions not
$this->includeFile = 'mpdf.php';
}
parent::__construct($phpWord);
parent::__construct($phpWord, $config);
}

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

$options = [];
$options = $this->config;
if ($this->getFont()) {
$options['default_font'] = $this->getFont();
}
7 changes: 5 additions & 2 deletions src/PhpWord/Writer/RTF.php
Original file line number Diff line number Diff line change
@@ -35,10 +35,13 @@ class RTF extends AbstractWriter implements WriterInterface

/**
* Create new instance.
*
* @param array $config
*/
public function __construct(?PhpWord $phpWord = null)
public function __construct(?PhpWord $phpWord = null, $config = [])
{
$this->setPhpWord($phpWord);
// Assign PhpWord
parent::__construct($phpWord, $config);

$this->parts = ['Header', 'Document'];
foreach ($this->parts as $partName) {
9 changes: 5 additions & 4 deletions src/PhpWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/PhpWord/Writer/Word2007.php

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @param has invalid value \(\\PhpOffice\\PhpWord\\PhpWord\)\: Unexpected token "\\n ", expected variable at offset 86$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/Word2007.php was not matched in reported errors.
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
@@ -44,12 +44,13 @@
/**
* Create new Word2007 writer.
*
* @param \PhpOffice\PhpWord\PhpWord
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @param array $config
*/
public function __construct(?PhpWord $phpWord = null)
public function __construct(?PhpWord $phpWord = null, $config = [])
{
// Assign PhpWord
$this->setPhpWord($phpWord);
parent::__construct($phpWord, $config);

// Create parts
// The first four files need to be in this order for Mimetype detection to work
@@ -78,7 +79,7 @@
foreach (array_keys($this->parts) as $partName) {
$partClass = static::class . '\\Part\\' . $partName;
if (class_exists($partClass)) {
/** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $part Type hint */
/** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $part */
$part = new $partClass();
$part->setParentWriter($this);
$this->writerParts[strtolower($partName)] = $part;
2 changes: 1 addition & 1 deletion tests/PhpWordTests/Writer/PDF/MPDFTest.php
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ public function testConstruct(): void
$section = $phpWord->addSection($oSettings); // @phpstan-ignore-line
$section->addText('Section 2 - landscape');

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

self::assertFileExists($file);