有中文要这写 切保证你的代码页面是编码是utf-8
$mpdf = new mPDF('zh-CN');
$mpdf->useAdobeCJK = true;
$mpdf->WriteHTML('中国');
$mpdf->Output();
安装mPDF
使用 Composer安装
在yii2的 ‘composer.json’ 加入"mpdf/mpdf"
:
"*"
1 |
"require" : {
|
2 |
"php" : ">=5.4.0" ,
|
3 |
"yiisoft/yii2" : "*" ,
|
4 |
"yiisoft/yii2-bootstrap" : "*" ,
|
5 |
"yiisoft/yii2-swiftmailer" : "*" ,
|
6 |
"mpdf/mpdf" : "*"
|
7 |
}, |
然后更新
1 |
composer |
或使用
1 |
php require mpdf/mpdf "dev-master"
|
或下载安装包
下载mpdf的地址 ‘https://github.com/mpdf/mpdf’. 下载压缩包 解压到vendor/mpdf
在这个文件中 ‘vendor/composer/autoload_namespaces.php’ file 加入
1 |
'mPDF' => array ( $vendorDir . '/mpdf/mpdf' ),
|
创建 PDF 使用 mPDF
SiteController.php
01 |
............... |
02 |
use app\models\ContactForm;
|
03 |
use mPDF;
|
04 |
class SiteController extends Controller
|
05 |
{ |
06 |
............... |
07 |
public function actionCreateMPDF(){
|
08 |
$mpdf = new mPDF();
|
09 |
$mpdf ->WriteHTML( $this ->renderPartial( 'mpdf' ));
|
10 |
$mpdf ->Output();
|
11 |
exit ;
|
12 |
//return $this->renderPartial('mpdf');
|
13 |
}
|
14 |
public function actionSamplePdf() {
|
15 |
$mpdf = new mPDF;
|
16 |
$mpdf ->WriteHTML( 'Sample Text' );
|
17 |
$mpdf ->Output();
|
18 |
exit ;
|
19 |
}
|
20 |
public function actionForceDownloadPdf(){
|
21 |
$mpdf = new mPDF();
|
22 |
$mpdf ->WriteHTML( $this ->renderPartial( 'mpdf' ));
|
23 |
$mpdf ->Output( 'MyPDF.pdf' , 'D' );
|
24 |
exit ;
|
25 |
}
|
26 |
............... |
27 |
} |