PHP code example of yii2tech / html2pdf
1. Go to this page and download the library: Download yii2tech/html2pdf library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2tech / html2pdf example snippets
return [
'components' => [
'html2pdf' => [
'class' => 'yii2tech\html2pdf\Manager',
'viewPath' => '@app/views/pdf',
'converter' => 'wkhtmltopdf',
],
],
...
];
$html = <<<HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Simple Content</p>
</body>
</html>
HTML;
// create PDF file from HTML content :
Yii::$app->html2pdf
->convert($html)
->saveAs('/path/to/output.pdf');
// convert HTML file to PDF file :
Yii::$app->html2pdf
->convertFile('/path/to/source.html')
->saveAs('/path/to/output.pdf');
Yii::$app->html2pdf
->convertFile('/path/to/source.html', ['pageSize' => 'A4'])
->saveAs('/path/to/output.pdf');
return [
'components' => [
'html2pdf' => [
'class' => 'yii2tech\html2pdf\Manager',
'viewPath' => '@app/pdf',
'converter' => [
'class' => 'yii2tech\html2pdf\converters\Wkhtmltopdf',
'defaultOptions' => [
'pageSize' => 'A4'
],
]
],
],
...
];
Yii::$app->html2pdf
->render('invoice', ['user' => Yii::$app->user->identity])
->saveAs('/path/to/output.pdf');
/* @var $this \yii\web\View */
/* @var $context \yii2tech\html2pdf\Template */
/* @var $user \app\models\User */
$context = $this->context;
$context->layout = 'layouts/payment'; // use specific layout for this template
// specify particular PDF conversion for this template:
$context->pdfOptions = [
'pageSize' => 'A4',
// ...
];
php composer.phar