Yii2用mpdf生成pdf 并用 swiftmailer 发送邮件

2016-5-9 Frank

  1. 通过composer安装依赖包
"require": {
        "yiisoft/yii2-swiftmailer": "*",        
        "kartik-v/yii2-mpdf": "dev-master"
    }, 

php composer.phar install

  1. 生成pdf并发送邮件
$content = "这是邮件内容" ;
$pdf = new Pdf([
    // set to use core fonts only
    'mode' => Pdf::MODE_UTF8,
    // A4 paper format
    'format' => Pdf::FORMAT_A4,
    // portrait orientation
    'orientation' => Pdf::ORIENT_LANDSCAPE,
    // stream to browser inline
    'destination' => Pdf::DEST_BROWSER,
    // your html content input
    'content' => '你好,世界',
    // format content from your own css file if needed or use the
    // enhanced bootstrap css built by Krajee for mPDF formatting
    'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
    // any css to be embedded if required
    'cssInline' => '.kv-heading-1{font-size:24px}',
    // set mPDF properties on the fly
    'options' => [
        'title' => 'VOUCHER',
        'autoLangToFont' => true,    //这几个配置加上可以显示中文
        'autoScriptToLang' => true,  //这几个配置加上可以显示中文
        'autoVietnamese' => true,    //这几个配置加上可以显示中文
        'autoArabic' => true,        //这几个配置加上可以显示中文
    ],
    // call mPDF methods on the fly
    'methods' => [
        'SetHeader' => ['VOUCHER'],
        'SetFooter' => ['{PAGENO}'],
    ]
]);
$pdf_as_string = $pdf->Output($content, '', 'S'); //这里是关键哦
$mailer = Yii::$app->mailer->compose('html', []);
$mailer->setFrom(["frank@liangcuntu.com" => "两寸土"]);
$mailer->setTo("frank@liangcuntu.com");
$mailer->setSubject("title");
$mailer->attachContent($pdf_as_string, ["fileName" => "test.pdf", "contentType" => "application/pdf"]);
$mailer->send(); 

参考:

https://github.com/yiisoft/yii2-swiftmailer

https://github.com/kartik-v/yii2-mpdf

« 上一篇:Node.js的log4j日志级别 | 下一篇:Node.js 调试»

评论:

两寸土
2016-05-09 19:58
$pdf->configure([
            'autoLangToFont' => true,    //这几个配置加上可以显示中文
            'autoScriptToLang' => true,  //这几个配置加上可以显示中文
            'autoVietnamese' => true,    //这几个配置加上可以显示中文
            'autoArabic' => true,        //这几个配置加上可以显示中文
        ]);
两寸土
2016-05-09 19:58
解决中文乱码:

发表评论 登录

Top