This is a basic wrapper around Snappy implemented as Yii application component. Snappy is a PHP library that uses wkhtmltopdf and wkhtmltoimage to render HTML into PDF and various image formats.
Installation using Composer should be straightforward
% composer require dmitrivereshchagin/yii-snappy
To register components in your application add something like the
following in configuration components section:
'pdf' => array(
'class' => 'dmitrivereshchagin\\yii\\snappy\\PdfComponent',
'binary' => '/usr/local/bin/wkhtmltopdf',
'options' => array('orientation' => 'landscape'),
'tempdir' => basename(__DIR__).'/runtime/pdf',
),
'image' => array(
'class' => 'dmitrivereshchagin\\yii\\snappy\\ImageComponent',
'binary' => '/usr/local/bin/wkhtmltoimage',
'tempdir' => basename(__DIR__).'/runtime/image',
),Specifying paths to binaries is mandatory. Optionally you can configure
default command line arguments that can be overridden or adjusted later.
It is also possible to set temporary directory for both components using
snappyTempdir application parameter.
Use Yii::app()->pdf and Yii::app()->image to generate PDF documents
and images respectively. These components provide access to methods
declared by \Knp\Snappy\GeneratorInterface.
Here are some examples.
Yii::app()->image->generate('http://example.com', '/path/to/image.jpg');Yii::app()->pdf->generate('http://example.com', '/path/to/document.pdf');Yii::app()->pdf->generate(
array('http://example.com', 'http://example.org'),
'/path/to/document.pdf'
);Yii::app()->pdf->generateFromHtml(
$this->render('view', array('name' => $value), $return = true),
'/path/to/document.pdf'
);$html = $this->render('view', array('name' => $value), $return = true);
Yii::app()->request->sendFile(
'document.pdf',
Yii::app()->pdf->getOutputFromHtml($html)
);$url = Yii::app()->createAbsoluteUrl('controller/action');
Yii::app()->request->sendFile(
'document.pdf',
Yii::app()->pdf->getOutput($url)
);This code uses Snappy library. Snappy has been originally developed by KnpLabs team.