Skip to content

Commit

Permalink
Merge pull request #2 from fagundes/develop
Browse files Browse the repository at this point in the history
v0.5.1
  • Loading branch information
fagundes committed Apr 23, 2016
2 parents b8f25a8 + be340f3 commit 99d93c3
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 52 deletions.
5 changes: 2 additions & 3 deletions README.md
Expand Up @@ -9,8 +9,8 @@ Zff\Html2Pdf
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffHtml2pdf/badge.svg?branch=develop&service=github)](https://coveralls.io/github/fagundes/ZffHtml2pdf?branch=develop)

[![Latest Stable Version](https://img.shields.io/packagist/v/fagundes/zff-html2pdf.svg)](https://packagist.org/packages/fagundes/zff-html2pdf)
[![Build Status](https://travis-ci.org/fagundes/ZffHtml2pdf.svg?branch=0.5.0)](https://travis-ci.org/fagundes/ZffHtml2pdf)
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffHtml2pdf/badge.svg?branch=0.5.0&service=github)](https://coveralls.io/github/fagundes/ZffHtml2pdf?branch=0.5.0)
[![Build Status](https://travis-ci.org/fagundes/ZffHtml2pdf.svg?branch=0.5.1)](https://travis-ci.org/fagundes/ZffHtml2pdf)
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffHtml2pdf/badge.svg?branch=0.5.1&service=github)](https://coveralls.io/github/fagundes/ZffHtml2pdf?branch=0.5.1)

- File issues at https://github.com/fagundes/ZffHtml2pdf/issues
- [Online documentation](https://fagundes.github.io/ZffHtml2pdf)
Expand All @@ -20,7 +20,6 @@ TODO List
---------

- [ ] Rewrite html2pdf examples using `Zff\Html2Pdf` (IN PROGRESS)
- [x] Create a way to easily change params from HTML2PDF's constructor, called on `Html2PdfRenderer` class, on controller, view or config file.

Contribuing
-----------
Expand Down
38 changes: 38 additions & 0 deletions doc/book/configuring.md
@@ -1,8 +1,12 @@
# Configuring html2pdf constructor

## 1. Default values from config file

Optionally, you can change the default values for html2pdf
constructor by adding this configuration to your `./config/autoload/global.php`.

Note, you don't need set all options, only those you need to override.

```php
<?php
return [
Expand All @@ -18,4 +22,38 @@ return [
],
],
];
```

## 2. Change default values in controller

Another way to change the default values for html2pdf constructor in a single action is overriding
the options by setting them in `Html2PdfModel` like below.

Note, these options will be merged with the options defined on config file.
It will override the config file when necessary.

```php
<?php

use Zff\Html2Pdf\View\Model\Html2PdfModel;

MyController
{

public function myAction()
{
$model = new Html2PdfModel();
$model->setHtml2PdfOptions([
'orientation' => 'P',
'format' => 'A4',
'lang' => 'en',
'unicode' => true,
'encoding' => 'UTF-8',
'margins' => [0, 0, 0, 0],
]);

return $model;
}
}

```
2 changes: 1 addition & 1 deletion doc/book/quick-start.md
Expand Up @@ -32,7 +32,7 @@ Controller: `AnyController.php`
<?php

use Zff\Html2Pdf\View\Model\Html2PdfModel;
`Html2PdfModel

class AnyController
{

Expand Down
23 changes: 3 additions & 20 deletions src/Html2PdfFactory.php
Expand Up @@ -14,21 +14,15 @@
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class Html2PdfFactory implements FactoryInterface
class Html2PdfFactory
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return Html2Pdf
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
public static function factory(array $options = null)
{
$defaultOptions = [
'orientation' => 'P',
Expand All @@ -50,15 +44,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$options['margins']
);
}

/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return Html2Pdf
*/
public function createService(ServiceLocatorInterface $serviceLocator, $cName = null, $rName = null)
{
return $this($serviceLocator, $rName);
}
}
}
11 changes: 3 additions & 8 deletions src/Mvc/Service/ViewHtml2PdfStrategyFactory.php
Expand Up @@ -43,8 +43,8 @@ public function createService(ServiceLocatorInterface $serviceLocator, $cName =
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @param string $requestedName
* @param null|array $options
* @return Html2PdfStrategy
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
Expand All @@ -55,16 +55,11 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
{
$config = $container->get('Config');

/**
* @var Html2PdfFactory $html2pdfFactory
*/
$html2pdfFactory = $container->get(Html2PdfFactory::class);
$html2pdfOptions = isset($config['zff-html2pdf']['options']) ? $config['zff-html2pdf']['options'] : [];
$html2pdf = $html2pdfFactory($container, Html2Pdf::class, $html2pdfOptions);

$html2pdfRenderer = new Html2PdfRenderer();
$html2pdfRenderer->setViewRenderer($container->get('ViewRenderer'));
$html2pdfRenderer->setHtml2Pdf($html2pdf);
$html2pdfRenderer->setDefaultHtml2pdfOptions($html2pdfOptions);

$html2pdfStrategy = new Html2PdfStrategy($html2pdfRenderer);

Expand Down
1 change: 0 additions & 1 deletion src/View/Model/Html2PdfModel.php
Expand Up @@ -119,5 +119,4 @@ public function setHtml2PdfOptions(array $html2PdfOptions)
{
$this->html2PdfOptions = $html2PdfOptions;
}

}
32 changes: 23 additions & 9 deletions src/View/Renderer/Html2PdfRenderer.php
Expand Up @@ -11,6 +11,7 @@
use Zend\View\Renderer\RendererInterface as Renderer;
use Zend\View\Renderer\PhpRenderer as ViewRenderer;
use Zend\View\Resolver\ResolverInterface as Resolver;
use Zff\Html2Pdf\Html2PdfFactory;
use Zff\Html2Pdf\View\Model\Html2PdfModel;

/**
Expand All @@ -24,9 +25,9 @@ class Html2PdfRenderer implements Renderer
protected $viewRenderer;

/**
* @var Html2Pdf
* @var array
*/
protected $html2pdf;
protected $defaultHtml2pdfOptions;

/**
* @return ViewRenderer
Expand All @@ -44,19 +45,19 @@ public function setViewRenderer(ViewRenderer $viewRenderer)
}

/**
* @return Html2Pdf
* @return array
*/
public function getHtml2pdf()
public function getDefaultHtml2pdfOptions()
{
return $this->html2pdf;
return $this->defaultHtml2pdfOptions;
}

/**
* @param Html2Pdf $html2pdf
* @param array $defaultHtml2pdfOptions
*/
public function setHtml2pdf($html2pdf)
public function setDefaultHtml2pdfOptions($defaultHtml2pdfOptions)
{
$this->html2pdf = $html2pdf;
$this->defaultHtml2pdfOptions = $defaultHtml2pdfOptions;
}

/**
Expand Down Expand Up @@ -101,7 +102,7 @@ public function getEngine()
*/
public function render($nameOrModel, $values = null)
{
$html2pdf = $this->getHtml2pdf();
$html2pdf = $this->createHtml2pdf($nameOrModel);

//set the variable html2pdf on the view
$nameOrModel->setVariable('html2pdf', $html2pdf);
Expand All @@ -115,4 +116,17 @@ public function render($nameOrModel, $values = null)
// send the PDF
return $html2pdf->Output($nameOrModel->getFilename(), $nameOrModel->getDest());
}

/**
* @param mixed $nameOrModel
* @return Html2Pdf
*/
protected function createHtml2pdf($nameOrModel)
{
$options = $nameOrModel instanceof Html2PdfModel && !empty($nameOrModel->getHtml2PdfOptions()) ?
$nameOrModel->getHtml2PdfOptions() :
$this->getDefaultHtml2pdfOptions();

return Html2PdfFactory::factory($options);
}
}
6 changes: 3 additions & 3 deletions src/View/Strategy/Html2PdfStrategy.php
Expand Up @@ -25,7 +25,7 @@ class Html2PdfStrategy extends AbstractListenerAggregate
/**
* Constructor
*
* @param Html2PdfRenderer $renderer
* @param Html2PdfRenderer $renderer
*/
public function __construct(Html2PdfRenderer $renderer)
{
Expand All @@ -36,7 +36,7 @@ public function __construct(Html2PdfRenderer $renderer)
* Attach the aggregate to the specified event manager
*
* @param EventManagerInterface $events
* @param int $priority
* @param int $priority
* @return void
*/
public function attach(EventManagerInterface $events, $priority = 1)
Expand Down Expand Up @@ -71,7 +71,7 @@ public function selectRenderer(ViewEvent $e)
* Populates the content of the response object from the view rendering
* results.
*
* @param ViewEvent $e
* @param ViewEvent $e
* @return void
*/
public function injectResponse(ViewEvent $e)
Expand Down
7 changes: 4 additions & 3 deletions test/ModuleTest.php
@@ -1,7 +1,7 @@
<?php
/**
* @license http://opensource.org/licenses/MIT MIT
* @copyright Copyright (c) 2015 Vinicius Fagundes
* @license http://opensource.org/licenses/MIT MIT
*/

namespace ZffTest\Html2Pdf;
Expand Down Expand Up @@ -34,7 +34,8 @@ public function testConfig()
$this->assertEquals($configArr, $this->module->getConfig());
}

public function testAutoloader() {
public function testAutoloader()
{
$this->assertNotEmpty($this->module->getAutoloaderConfig());
}
}
}
3 changes: 1 addition & 2 deletions test/Mvc/Service/ViewHtml2PdfStrategyFactoryTest.php
Expand Up @@ -44,5 +44,4 @@ public function testCanCreateHtml2PdfStrategyFromServiceManager()
{
$this->assertInstanceOf(Html2PdfStrategy::class, $this->serviceManager->get('ViewHtml2PdfStrategy'));
}

}
}
2 changes: 1 addition & 1 deletion test/View/Model/Html2PdfModelTest.php
Expand Up @@ -23,7 +23,7 @@ class Html2PdfModelTest extends TestCase
protected function setUp()
{
$this->model = new Html2PdfModel([
'foo' => 'bar'
'foo' => 'bar',
]);

$this->model->setFilename('myNewDoc.pdf');
Expand Down
30 changes: 29 additions & 1 deletion test/View/Renderer/Html2PdfRendererTest.php
Expand Up @@ -31,7 +31,7 @@ public function setUp()
{
$this->phpRenderer = new PhpRenderer;
$this->renderer = new Html2PdfRenderer();
$this->renderer->setHtml2pdf(new \HTML2PDF());
$this->renderer->setDefaultHtml2pdfOptions(array());

$this->renderer->setViewRenderer($this->phpRenderer);
}
Expand Down Expand Up @@ -85,4 +85,32 @@ public function testPassingValidHtmlToRenderAsString()

$this->assertStringStartsWith('%PDF', $pdfContentAsString);
}

public function testMergeConstructorOptionsFromModel()
{
$model = new Html2PdfModel();
$model->setDest('S');
$model->setTemplate('template00.phtml');
$model->setHtml2PdfOptions([
'orientation' => 'L',
'format' => 'A3',
'lang' => 'pt',
'encoding' => 'ISO-8859-1',
'margins' => [5, 10, 20, 30],
]);

$this->renderer->resolver()->addPath('./test/_templates');
$this->renderer->render($model);

$html2pdfObject = $model->getVariable('html2pdf');

$this->assertNotNull($html2pdfObject);

$this->assertAttributeEquals('L', '_orientation', $html2pdfObject);
$this->assertAttributeEquals('A3', '_format', $html2pdfObject);
$this->assertAttributeEquals('pt', '_langue', $html2pdfObject);
$this->assertAttributeEquals(true, '_unicode', $html2pdfObject);
$this->assertAttributeEquals('ISO-8859-1', '_encoding', $html2pdfObject);
}

}

0 comments on commit 99d93c3

Please sign in to comment.