Skip to content

Commit

Permalink
Enhance ExportController HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Oct 9, 2018
1 parent b437e9f commit 3183dd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGE.md
Expand Up @@ -3,8 +3,9 @@ Change Log: `yii2-grid`

## Version 3.2.6

**Date:** 29-Sep-2018
**Date:** _under development_

- Enhance `ExportController` HTTP headers.
- (enh #837): Various enhancements to grid export styling.
- (bug #835): Correct rendering error due to code change in core `GridView` in `yiisoft/yii2`.

Expand Down
8 changes: 4 additions & 4 deletions src/GridView.php
Expand Up @@ -1058,6 +1058,10 @@ protected static function initCss(&$options, $css)
public function init()
{
$this->initModule();
if (isset($this->_module->bsVersion)) {
$this->bsVersion = $this->_module->bsVersion;
}
$this->initBsVersion();
Html::addCssClass($this->options, 'is-bs' . ($this->isBs4() ? '4' : '3'));
if (empty($this->options['id'])) {
$this->options['id'] = $this->getId();
Expand Down Expand Up @@ -1451,10 +1455,6 @@ protected function initModule()
if (isset($this->bsVersion)) {
return;
}
if (isset($this->_module->bsVersion)) {
$this->bsVersion = $this->_module->bsVersion;
}
$this->initBsVersion();
}

/**
Expand Down
28 changes: 12 additions & 16 deletions src/controllers/ExportController.php
Expand Up @@ -11,6 +11,7 @@

use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\Response;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function actionDownload()
* @param array $config the configuration for yii2-mpdf component
*
* @return Response
* @throws InvalidConfigException
*/
protected function generatePDF($content, $filename, $config = [])
{
Expand All @@ -88,7 +90,6 @@ protected function generatePDF($content, $filename, $config = [])
$config['methods']['SetCreator'] = [Yii::t('kvgrid', 'Krajee Yii2 Grid Export Extension')];
$config['content'] = $content;
$pdf = new Pdf($config);
Yii::$app->response->format = Response::FORMAT_RAW;
return $pdf->render();
}

Expand All @@ -99,23 +100,18 @@ protected function generatePDF($content, $filename, $config = [])
* @param string $name the file name
* @param string $mime the mime time for the file
* @param string $encoding the encoding for the file content
*
* @return void
*/
protected function setHttpHeaders($type, $name, $mime, $encoding = 'utf-8')
{
Yii::$app->response->format = Response::FORMAT_RAW;
if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') == false) {
header('Cache-Control: no-cache');
header('Pragma: no-cache');
} else {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}
header('Expires: Sat, 26 Jul 1979 05:00:00 GMT');
header("Content-Encoding: {$encoding}");
header("Content-Type: {$mime}; charset={$encoding}");
header("Content-Disposition: attachment; filename={$name}.{$type}");
header('Cache-Control: max-age=0');
$response = Yii::$app->response;
$response->format = Response::FORMAT_RAW;
$headers = $response->getHeaders();
$headers->set('Content-Type', "{$mime}; charset={$encoding}");
$headers->set('Content-Transfer-Encoding', $encoding);
$headers->set('Cache-Control', 'public, must-revalidate, max-age=0');
$headers->set('Pragma', 'public');
$headers->set('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');
$headers->set('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$headers->set('Content-Disposition', "attachment; filename={$name}.{$type}");
}
}

0 comments on commit 3183dd4

Please sign in to comment.