DompdfHelper - a lightweight library wrapper Laminas module
Installation of DompdfHelper uses PHP Composer. For more information about PHP Composer, please visit the official PHP Composer site.
-
cd my/project/directory
-
create a
composer.json
file with following contents:{ "require": { "rarog/dompdf-helper": "^4.0" } }
-
install PHP Composer via
curl -s http://getcomposer.org/installer | php
(on windows, download http://getcomposer.org/installer and execute it with PHP) -
run
php composer.phar install
-
open
my/project/directory/config/application.config.php
and add the following key to yourmodules
:'DompdfHelper',
You can override default options via the dompdf
key in your local or global config files. See the config/dompdf.config.php.dist file for the list of default settings.
Full list of possible settings is available at the official Dompdf library site.
Controller factory
<?php
namespace My\Factory\Controller;
use Interop\Container\ContainerInterface;
use My\Controller\ExampleController;
class ExampleControllerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke()
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ExampleController(
$container->get('dompdf')
);
}
}
Controller
<?php
namespace My\Controller;
use Dompdf\Dompdf;
use Laminas\Mvc\Controller\AbstractActionController;
class ExampleController extends AbstractActionController
{
/**
* @var Dompdf
*/
private $dompdf;
/**
* Constructor
*
* @param Dompdf $dompdf
*/
public function __construct(
Dompdf $dompdf
) {
$this->dompdf = $dompdf;
}
public function indexAction()
{
$this->dompdf->load_html('<strong>Hello World</strong>');
$this->dompdf->render();
file_put_contents(__DIR__ . '/document.pdf', $this->dompdf->output());
}
}