|
1 | 1 | # yii-snappy |
2 | | -Basic wrapper around [Snappy](https://github.com/KnpLabs/snappy) implemented as Yii component |
| 2 | + |
| 3 | +This is a basic wrapper around [Snappy] [s] implemented as [Yii] [y] |
| 4 | +application component. Snappy is a PHP library that uses |
| 5 | +[wkhtmltopdf and wkhtmltoimage] [w] to render HTML into PDF and various |
| 6 | +image formats. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Installation using [Composer] [c] should be straightforward |
| 11 | + |
| 12 | +``` |
| 13 | +composer require dmitrivereshchagin/yii-snappy |
| 14 | +``` |
| 15 | + |
| 16 | +To register components in your application add something like the |
| 17 | +following in configuration `components` section: |
| 18 | + |
| 19 | +```php |
| 20 | +'pdf' => array( |
| 21 | + 'class' => '\Snappy\PdfComponent', |
| 22 | + 'binary' => '/usr/local/bin/wkhtmltopdf', |
| 23 | + 'options' => array('orientation' => 'landscape'), |
| 24 | +), |
| 25 | +'image' => array( |
| 26 | + 'class' => '\Snappy\ImageComponent', |
| 27 | + 'binary' => '/usr/local/bin/wkhtmltoimage', |
| 28 | +), |
| 29 | +``` |
| 30 | + |
| 31 | +Specifying paths to binaries is mandatory. Optionally you can configure |
| 32 | +default command line arguments that can be overridden or adjusted later. |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +Use `Yii::app()->pdf` and `Yii::app()->image` to generate PDF documents |
| 37 | +and images respectively. These components provide access to methods |
| 38 | +declared by `\Knp\Snappy\GeneratorInterface`. |
| 39 | + |
| 40 | +Here are some examples. |
| 41 | + |
| 42 | +### Generate image from an URL |
| 43 | + |
| 44 | +```php |
| 45 | +Yii::app()->image->generate('http://example.com', '/path/to/image.jpg'); |
| 46 | +``` |
| 47 | + |
| 48 | +### Generate PDF document from an URL |
| 49 | + |
| 50 | +```php |
| 51 | +Yii::app()->pdf->generate('http://example.com', '/path/to/document.pdf'); |
| 52 | +``` |
| 53 | + |
| 54 | +### Generate PDF document from multiple URLs |
| 55 | + |
| 56 | +```php |
| 57 | +Yii::app()->pdf->generate( |
| 58 | + array('http://example.com', 'http://example.org'), |
| 59 | + '/path/to/document.pdf' |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +### Generate PDF document from a view |
| 64 | + |
| 65 | +```php |
| 66 | +Yii::app()->pdf->generateFromHtml( |
| 67 | + $this->render('view', array('name' => $value), $return = true), |
| 68 | + '/path/to/document.pdf' |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +### Generate PDF document as response from controller |
| 73 | + |
| 74 | +```php |
| 75 | +$html = $this->render('view', array('name' => $value), $return = true); |
| 76 | + |
| 77 | +Yii::app()->request->sendFile( |
| 78 | + 'document.pdf', |
| 79 | + Yii::app()->pdf->getOutputFromHtml($html) |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +### Generate PDF document with relative URLs inside |
| 84 | + |
| 85 | +```php |
| 86 | +$url = Yii::app()->createAbsoluteUrl('controller/action'); |
| 87 | + |
| 88 | +Yii::app()->request->sendFile( |
| 89 | + 'document.pdf', |
| 90 | + Yii::app()->pdf->getOutput($url) |
| 91 | +); |
| 92 | +``` |
| 93 | + |
| 94 | +## Credits |
| 95 | + |
| 96 | +This code is based on Snappy library. Snappy has been originally |
| 97 | +developed by [KnpLabs] [k] team. |
| 98 | + |
| 99 | +[c]: https://getcomposer.org |
| 100 | +[k]: http://knplabs.com |
| 101 | +[s]: https://github.com/KnpLabs/snappy |
| 102 | +[w]: http://wkhtmltopdf.org |
| 103 | +[y]: https://github.com/yiisoft/yii |
0 commit comments