Skip to content

Commit

Permalink
Fix #326: Correct PDF writer export for latest PHP Spreadsheet release
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Jul 27, 2020
1 parent 14f3a93 commit 29fc213
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 103 deletions.
5 changes: 4 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ Change Log: `yii2-export`

## version 1.4.1

**Date:** _under development_
**Date:** 27-Jul-2020

- (enh #326): Correct PDF writer export for latest PHP Spreadsheet release.
- (enh #323): Void return added for PHPSpreadsheet save method.
- (enh #321): Update Greek Translations.
- (enh #319): Enhancements of export column selector.
- (enh #318): Correct icons for bootstrap 4.
- (enh #317): Get export type function.
Expand Down
107 changes: 5 additions & 102 deletions src/ExportWriterPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

namespace kartik\export;

use DOMDocument;
use kartik\mpdf\Pdf;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;

/**
* Krajee custom PDF Writer library based on MPdf
Expand All @@ -37,105 +34,11 @@ class ExportWriterPdf extends Mpdf
*/
protected function createExternalWriterInstance($config = [])
{
$config = array_replace_recursive($config, $this->pdfConfig);
return new Pdf($config);
}

/**
* Save Spreadsheet to file.
*
* @param string $pFilename Name of the file to save as
*
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @throws PhpSpreadsheetException
* @throws \yii\base\InvalidConfigException
*/
public function save($pFilename): void
{
$fileHandle = parent::prepareForSave($pFilename);

// Default PDF paper size
$paperSize = Pdf::FORMAT_A4;

// Check for paper size and page orientation
if (null === $this->getSheetIndex()) {
$orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
$printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
} else {
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
}
$this->setOrientation($orientation);

// Override Page Orientation
if (null !== $this->getOrientation()) {
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
? PageSetup::ORIENTATION_PORTRAIT
: $this->getOrientation();
}
$orientation = strtoupper($orientation);

// Override Paper Size
if (null !== $this->getPaperSize()) {
$printPaperSize = $this->getPaperSize();
}

if (isset(self::$paperSizes[$printPaperSize])) {
$paperSize = self::$paperSizes[$printPaperSize];
}

$properties = $this->spreadsheet->getProperties();

// Create PDF
$pdf = $this->createExternalWriterInstance([
'format' => $paperSize,
'orientation' => $orientation,
'methods' => [
'SetTitle' => $properties->getTitle(),
'SetAuthor' => $properties->getCreator(),
'SetSubject' => $properties->getSubject(),
'SetKeywords' => $properties->getKeywords(),
'SetCreator' => $properties->getCreator(),
],
]);
$content = $this->generateHTMLHeader(false) . $this->generateSheetData() . $this->generateHTMLFooter();
// Write to file
fwrite($fileHandle, $pdf->output(static::cleanHTML($content), $this->filename, Pdf::DEST_STRING));
parent::restoreStateAfterSave($fileHandle);
}

/**
* Cleans HTML of embedded script, style, and link tags
*
* @param string $content the source HTML content
* @return string the cleaned HTML content
*/
protected static function cleanHTML($content)
{
if (empty($content)) {
return $content;
}
$doc = new DOMDocument();
$doc->loadHTML($content);
static::removeElementsByTagName('script', $doc);
static::removeElementsByTagName('style', $doc);
static::removeElementsByTagName('link', $doc);
return $doc->saveHTML();
}

/**
* Remove DOM elements by tag name
* @param string $tagName the tag name to parse and remove
* @param DOMDocument $document the DomDocument object
*/
protected static function removeElementsByTagName($tagName, $document)
{
$nodeList = $document->getElementsByTagName($tagName);
for ($nodeIdx = $nodeList->length; --$nodeIdx >= 0;) {
$node = $nodeList->item($nodeIdx);
$node->parentNode->removeChild($node);
if (isset($config['tempDir'])) {
unset($config['tempDir']);
}
$config = array_replace_recursive($config, $this->pdfConfig);
$pdf = new Pdf($config);
return $pdf->getApi();
}
}

0 comments on commit 29fc213

Please sign in to comment.