https://github.com/boundstate/yii2-htmlconverter
https://github.com/boundstate/yii2-htmlconverter
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/boundstate/yii2-htmlconverter
- Owner: boundstate
- Created: 2015-05-14T22:26:53.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-14T22:28:24.000Z (about 11 years ago)
- Last Synced: 2025-03-19T03:41:33.285Z (over 1 year ago)
- Language: PHP
- Size: 113 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yii2-htmlconverter
Extension for the Yii2 framework that converts HTML to PDF or images using [wkhtmltopdf].
## Installation
This extensions relies on `wkhtmltopdf`. Installation insructions are provided on the [wkhtmltopdf website] [wkhtmltopdf].
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
php composer.phar require --prefer-dist boundstate/yii2-htmlconverter "*"
or add
"boundstate/yii2-htmlconverter": "*"
to the require section of your `composer.json` file.
## Usage
Setup the components in your config:
'htmlToPdf' => [
'class' => 'boundstate\htmlconverter\HtmlToPdfConverter',
'bin' => '/usr/bin/wkhtmltopdf',
// global wkhtmltopdf command line options
// (see http://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
'options' => [
'print-media-type',
'disable-smart-shrinking',
'no-outline',
'page-size' => 'letter',
'load-error-handling' => 'ignore',
'load-media-error-handling' => 'ignore'
],
],
'htmlToImage' => [
'class' => 'boundstate\htmlconverter\HtmlToImageConverter',
'bin' => '/usr/bin/wkhtmltoimage',
],
'response' => [
'formatters' => [
'pdf' => [
'class' => 'boundstate\htmlconverter\PdfResponseFormatter',
// Set a filename to download the response as an attachments (instead of displaying in browser)
'filename' => 'attachment.pdf'
],
'image' => [
'class' => 'boundstate\htmlconverter\ImageResponseFormatter',
],
]
],
Now you can format a response as a PDF:
Yii::$app->response->format = 'pdf';
Or format a response as an image:
Yii::$app->response->format = 'image';
You can also manually generate a PDF from HTML:
$html = $this->render('hello-word');
$header = $this->render('hello-world-header');
$pdf = Yii::$app->htmlToPdf->convert($html, ['page-size' => 'A4', 'header-html' => $header]);
Or manually generate an image from HTML:
$html = $this->render('hello-word');
$pdf = Yii::$app->htmlToImage->convert($html);
[wkhtmltopdf]: http://wkhtmltopdf.org/