Skip to content

Commit

Permalink
Fix #273: Enhanced PDF Writer ExportWriterPdf
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Oct 11, 2018
1 parent 249cb9f commit 8ef8f78
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 65 deletions.
3 changes: 2 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Change Log: `yii2-export`

## version 1.3.4

**Date:** _under development_
**Date:** 12-Oct-2018

- (enh #273): Enhanced PDF Writer `ExportWriterPdf`.
- (enh #272): UTF-8 encoding for HTML, CSV, TEXT formats.
- (enh #271): Locale specific validation messages and code enhancements.
- (enh #270): Add iframe as default target for export form download.
Expand Down
79 changes: 15 additions & 64 deletions src/ExportMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
use kartik\dialog\Dialog;
use kartik\dynagrid\Dynagrid;
use kartik\grid\GridView;
use kartik\mpdf\Pdf;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter;
use PhpOffice\PhpSpreadsheet\Writer\Csv as WriterCsv;
use PhpOffice\PhpSpreadsheet\Writer\Html as WriterHtml;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
Expand Down Expand Up @@ -801,20 +798,13 @@ public function run()
if ($this->stream) {
$this->clearOutputBuffers();
$this->setHttpHeaders();
if ($this->_exportType === self::FORMAT_PDF) {
$this->renderPDF($file);
} else {
readfile($file);
}
readfile($file);
$this->cleanup($file, $config);
exit();
} else {
$this->registerAssets();
echo $this->renderExportMenu();
if ($this->_triggerDownload && $this->afterSaveView !== false) {
if ($this->_exportType === self::FORMAT_PDF) {
$this->renderPDF($file);
}
$config = ArrayHelper::getValue($this->exportConfig, $this->_exportType, []);
if (!empty($config)) {
$l = $this->linkFileName;
Expand Down Expand Up @@ -1117,11 +1107,22 @@ public function initPhpSpreadsheet()
*/
public function initPhpSpreadsheetWriter($type)
{
$t = $this->_exportType;
if ($t === self::FORMAT_PDF) {
IOFactory::registerWriter($type, ExportWriterPdf::class);
}
$writer = $this->_objWriter = IOFactory::createWriter($this->_objSpreadsheet, $type);
if ($t === self::FORMAT_PDF && !empty($this->exportConfig[$t])) {
$cfg = $this->exportConfig[$t];
/**
* @var ExportWriterPdf $writer
*/
$writer->filename = $this->filename . '.' . ArrayHelper::getValue($cfg, 'extension', 'pdf');
$writer->pdfConfig = ArrayHelper::getValue($cfg, 'pdfConfig', []);
}
/**
* @var WriterCsv $writer
*/
$writer = $this->_objWriter = IOFactory::createWriter($this->_objSpreadsheet, $type);
$t = $this->_exportType;
if ($t === self::FORMAT_TEXT) {
$delimiter = $this->getSetting('delimiter', "\t");
$writer->setDelimiter($delimiter);
Expand Down Expand Up @@ -1641,56 +1642,6 @@ protected function getSetting($key, $default = null)
return ArrayHelper::getValue($settings, $key, $default);
}

/**
* Parse PDF
*
* @param string $file the output filename on server with path
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @throws InvalidConfigException
*/
protected function renderPDF($file)
{
// Default PDF paper size
$spreadsheet = $this->_objSpreadsheet;
$sheet = $this->_objWorksheet;
/**
* @var WriterHtml $w
*/
$w = $this->_objWriter;
$page = $sheet->getPageSetup();
$orientation = $page->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE ? 'L' : 'P';
$properties = $spreadsheet->getProperties();
$settings = ArrayHelper::getValue($this->exportConfig, $this->_exportType, []);
$useInlineCss = ArrayHelper::getValue($settings, 'useInlineCss', false);
$config = ArrayHelper::getValue($settings, 'pdfConfig', []);
$w->setUseInlineCss($useInlineCss);
$config = array_replace_recursive(
[
'orientation' => strtoupper($orientation),
'methods' => [
'SetTitle' => $properties->getTitle(),
'SetAuthor' => $properties->getCreator(),
'SetCreator' => $properties->getCreator(),
'SetSubject' => $properties->getSubject(),
'SetKeywords' => $properties->getKeywords(),
],
'cssFile' => '',
'content' => $w->generateHTMLHeader(false) . $w->generateSheetData() . $w->generateHTMLFooter(),
],
$config
);
if (!$this->stream) {
$config['destination'] = Pdf::DEST_FILE;
$config['filename'] = $file;
} else {
$config['destination'] = Pdf::DEST_DOWNLOAD;
$extension = ArrayHelper::getValue($settings, 'extension', 'pdf');
$config['filename'] = $this->filename . '.' . $extension;
}
$pdf = new Pdf($config);
echo $pdf->render();
}

/**
* Initialize columns selected for export
*/
Expand Down Expand Up @@ -1865,7 +1816,7 @@ protected function setDefaultExportConfig()
'alertMsg' => Yii::t('kvexport', 'The PDF export file will be generated for download.'),
'mime' => 'application/pdf',
'extension' => 'pdf',
'writer' => self::FORMAT_HTML,
'writer' => 'KrajeePdf', // custom Krajee PDF writer using MPdf library
'useInlineCss' => true,
'pdfConfig' => [],
],
Expand Down
115 changes: 115 additions & 0 deletions src/ExportWriterPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2018
* @package yii2-export
* @version 1.3.4
*/

namespace kartik\export;

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
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class ExportWriterPdf extends Mpdf
{
/**
* @var string the exported output file name. Defaults to 'grid-export';
*/
public $filename;

/**
* @var array kartik\mpdf\Pdf component configuration settings
*/
public $pdfConfig = [];

/**
* @inheritdoc
*/
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
*/
public function save($pFilename)
{
$fileHandle = parent::prepareForSave($pFilename);

// Default PDF paper size
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)

// 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([
'orientation' => $orientation,
'methods' => [
'SetTitle' => $properties->getTitle(),
'SetAuthor' => $properties->getCreator(),
'SetSubject' => $properties->getSubject(),
'SetKeywords' => $properties->getKeywords(),
'SetCreator' => $properties->getCreator(),
],
]);
$ortmp = $orientation;
$lib = $pdf->getApi();
/** @noinspection PhpUndefinedMethodInspection */
$lib->_setPageSize(strtoupper($paperSize), $ortmp);
$lib->DefOrientation = $orientation;
/** @noinspection PhpUndefinedMethodInspection */
$lib->AddPage($orientation);
$content = strtr($this->generateHTMLHeader(false) . $this->generateSheetData() . $this->generateHTMLFooter(), [
'@page { margin-left: 0.7in; margin-right: 0.7in; margin-top: 0.75in; margin-bottom: 0.75in; }' => '',
'body { margin-left: 0.7in; margin-right: 0.7in; margin-top: 0.75in; margin-bottom: 0.75in; }' => '',
]);
// Write to file
fwrite($fileHandle, $pdf->Output($content, $this->filename, Pdf::DEST_STRING));
parent::restoreStateAfterSave($fileHandle);
}
}

0 comments on commit 8ef8f78

Please sign in to comment.