Skip to content

Commit

Permalink
add pdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciki committed Mar 21, 2019
1 parent 90ab348 commit 5563978
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Components/Exports/BaseExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ abstract class BaseExport extends Component implements IResponse
/** @var string */
private $title;

/** @var ?string */
private $filename;

/**
* @param string $label
*/
public function __construct($label = NULL)
public function __construct($label = NULL, $filename = NULL)
{
$this->label = $label;
$this->filename = $filename;
$this->monitor('Grido\Grid');
}

Expand Down Expand Up @@ -146,7 +150,7 @@ public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $h
? ucfirst(Strings::webalize($this->label))
: ucfirst($this->grid->name);

$this->setHttpHeaders($httpResponse, $label);
$this->setHttpHeaders($httpResponse, $this->filename ?: $label);

print chr(0xEF) . chr(0xBB) . chr(0xBF); //UTF-8 BOM
$this->printData();
Expand Down
87 changes: 87 additions & 0 deletions src/Components/Exports/PdfExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Grido\Components\Exports;

use Grido\Components\Columns\Column;
use Nette\Http\IResponse;

class PdfExport extends BaseExport
{

/**
* @return void
*/
protected function printData()
{
$columns = $this->grid[Column::ID]->getComponents();
$header = [];
$headerItems = $this->header ? $this->header : $columns;
foreach ($headerItems as $column) {
$header[] = $this->header ? $column : $column->getLabel();
}

$datasource = $this->grid->getData(FALSE, FALSE, FALSE);
$iterations = ceil($datasource->getCount() / $this->fetchLimit);
$formattedData = [];
$sums = [];
for ($i = 0; $i < $iterations; $i++) {
$datasource->limit($i * $this->fetchLimit, $this->fetchLimit);
$data = $this->customData ? call_user_func_array($this->customData, [$datasource]) : $datasource->getData();

foreach ($data as $items) {
$row = [];

$columns = $this->customData ? $items : $columns;

foreach ($columns as $columnName => $column) {
$row[$columnName] = $this->customData ? $column : $column->renderExport($items);
if ($column instanceof \Ciki\Grido\ColumnNumber && $column->getCalculateSum()) {
if (!isset($sums[$columnName])) {
$sums[$columnName] = 0;
}
// dump($row[$columnName], $column->getValueForSumCalculation($items), $column->getValue($items));
// $sums[$columnName] += $column->getValueForSumCalculation($items);
$sums[$columnName] += $row[$columnName] * 100; // => cents
}
}
$formattedData[] = $row;
}
}

// Tip: In template to make a new page use <pagebreak>
$template = $this->getPresenter()->getTemplate();
$template->setFile(__DIR__ . '/pdf_export.latte');
$template->header = $header;
$template->data = \Nette\Utils\ArrayHash::from($formattedData);
$template->sums = \Nette\Utils\ArrayHash::from($sums);
$template->columns = $columns;
// dump($header, $row, $data, $columns, $template->data, $sums);die;

$pdf = new \Joseki\Application\Responses\PdfResponse($template);

// optional
// $pdf->documentTitle = date("Y-m-d H:i") . " PDF export"; // creates filename
$pdf->pageFormat = "A4-L"; // wide format
$pdf->getMPDF()->setFooter("|© www.kartamesta.sk|");
// $pdf->outputDestination = $pdf::OUTPUT_DOWNLOAD;
// $pdf->save = $pdf::OUTPUT_DOWNLOAD;
echo $pdf->__toString();
}


/**
* @param IResponse $httpResponse
* @param string $label
*/
protected function setHttpHeaders(IResponse $httpResponse, $label)
{
$encoding = 'utf-8';
$httpResponse->setHeader('Content-Description', 'File Transfer');
$httpResponse->setHeader('Content-Transfer-Encoding', 'binary');
$httpResponse->setHeader('Content-Encoding', $encoding);
$httpResponse->setHeader('Content-Type', "application/pdf; charset=$encoding");
$httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$label.pdf\"");
}


}
45 changes: 45 additions & 0 deletions src/Components/Exports/pdf_export.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{**
* @param array $header public header names
* @param Nette\Utils\ArrayHash $data
* @param Nette\Utils\ArrayHash $sums
* @param RecursiveComponentIterator $columns
*}

{*extends TPL_INC_DIR . '/@styledBareLayout.latte'*}

{*block #body*}

<style>
body {
font-size:90%;
}
h2 {
font-size:18px;
}
/* original have line separator specified via unsupported rule, thus we are shrinking padding here */
table.table tbody td,
table.table thead th {
padding: 6px;
}
</style>


<table class="table">
<thead>
<tr>
<th n:foreach="$header as $h">{$h}</th>
</tr>
</thead>
<tbody>
<tr n:foreach="$data as $v">
<td n:foreach="$v as $v2">{$v2}</td>
</tr>
<tr n:if="!empty($sums)">
<td n:foreach="$columns as $column">
{ifset $sums[$column->name]}
&sum; {$column->renderSum($sums[$column->name] / 100)}
{/ifset}
</td>
</tr>
</tbody>
</table>
10 changes: 6 additions & 4 deletions src/templates/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@
{/block}
{input $form[buttons][perPage], class => 'hide'}
</span>
<span n:if="$control->hasExport()" class="export" n:inner-block="export" n:foreach="$control->getExports() as $export">
{var $title = $export->title ? : $template->translate('Grido.ExportAllItems')}
<a n:class="$customization->buttonClass" href="{=$export->link('export!')}" title="{$title}"><i class="{=$customization->getIconClass('download')}"> {_'Grido.Export'}</i></a>
</span>
{if $control->hasExport()}
<span class="export" n:inner-block="export" n:foreach="$control->getExports() as $export">
{var $title = $export->title ? : $template->translate('Grido.ExportAllItems')}
<a n:class="$customization->buttonClass" href="{=$export->link('export!')}" title="{$title}"><i class="{=$customization->getIconClass('download')}"> {_'Grido.Export'}</i></a>
</span>
{/if}
<span n:block="reset" class="reset">
{input $form[buttons][reset]}
</span>
Expand Down

0 comments on commit 5563978

Please sign in to comment.