Skip to content

Commit

Permalink
improvements:
Browse files Browse the repository at this point in the history
- added config tempDir for mPDF (closes #22) thx @romanmatyus
- typo
  • Loading branch information
petrparolek committed Mar 30, 2020
1 parent 0c3682c commit 1faaf7c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function actionPdf()
$pdf->documentTitle = date("Y-m-d") . " My super title"; // creates filename 2012-06-30-my-super-title.pdf
$pdf->pageFormat = "A4-L"; // wide format
$pdf->getMPDF()->setFooter("|© www.mysite.com|"); // footer

// do something with $pdf
$this->sendResponse($pdf);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public function actionPdf()
$pdf->setSaveMode(PdfResponse::INLINE);
$this->sendResponse($pdf);
}
```
```

Set a pdf background easily
---
Expand Down Expand Up @@ -152,6 +152,17 @@ public function actionPdf()
}
```

Configuration of custom temp dir for mPDF in PdfResponse
---

```yml
services:
-
factory: Joseki\Application\Responses\PdfResponse
setup:
- $mpdfConfig([tempDir: %tempDir%/mpdf])
```
See also
---
Expand Down
39 changes: 27 additions & 12 deletions src/PdfResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @author Tomáš Votruba
* @author Miroslav Paulík
* @author Štěpán Škorpil
* @author Petr Parolek
* @copyright Copyright (c) 2010 Jan Kuchař (http://mujserver.net)
* @license LGPL
* @link http://addons.nette.org/cs/pdfresponse2
Expand Down Expand Up @@ -74,6 +75,9 @@ class PdfResponse implements Nette\Application\IResponse
public const LAYOUT_TWORIGHT = "tworight"; // Display the pages in two columns, with the first page displayed on the right side (mPDF >= 5.2)
public const LAYOUT_DEFAULT = "default"; // User’s default setting in Adobe Reader

/** @var array */
public $mpdfConfig = array();

/** @var array onBeforeComplete event */
public $onBeforeComplete = array();

Expand Down Expand Up @@ -124,6 +128,24 @@ class PdfResponse implements Nette\Application\IResponse

/************************************ properties **************************************/

/**
* @param Template|string $source
* @throws InvalidArgumentException
*/
public function setTemplate($source)
{
if (!($source instanceof Template) && !is_string($source)) {
throw new InvalidArgumentException(
sprintf(
'Invalid source type. Expected (html) string or instance of Nette\Bridges\ApplicationLatte\Template, but "%s" given.',
is_object($source) ? get_class($source) : gettype($source)
)
);
}
$this->source = $source;
return $this;
}

/**
* @return string
*/
Expand Down Expand Up @@ -211,7 +233,7 @@ public function setDisplayLayout(string $displayLayout): void
self::LAYOUT_TWOLEFT,
self::LAYOUT_TWORIGHT
),
true
true
)
) {
throw new InvalidArgumentException("Invalid layout '$displayLayout', use PdfResponse::LAYOUT* constants.");
Expand Down Expand Up @@ -420,7 +442,7 @@ public function setBackgroundTemplate(string $pathToBackgroundTemplate): void
protected function getMPDFConfig(): array
{
$margins = $this->getMargins();
return [
return $this->mpdfConfig + [
'mode' => 'utf-8',
'format' => $this->pageFormat,
'margin_left' => $margins["left"],
Expand Down Expand Up @@ -464,21 +486,14 @@ public function getMPDF(): Mpdf
* @param Template|string $source
* @throws InvalidArgumentException
*/
public function __construct($source)
public function __construct($source = null)
{
if (!($source instanceof Template) && !is_string($source)) {
throw new InvalidArgumentException(
sprintf(
'Invalid source type. Expected (html) string or instance of Nette\Bridges\ApplicationLatte\Template, but "%s" given.',
is_object($source) ? get_class($source) : gettype($source)
)
);
if ($source !== NULL) {
$this->setTemplate($source);
}
$this->source = $source;
}



/*********************************** build **************************************/

/**
Expand Down

0 comments on commit 1faaf7c

Please sign in to comment.