【问题标题】:convert to pdf ( yii2)转换为 pdf (yii2)
【发布时间】:2016-11-10 14:32:17
【问题描述】:

当用户单击配置文件按钮时如何制作,它将显示在pdf文件中。我已经安装了 mpdf 扩展。下面是我尝试将personalinfo.php页面转换为pdf的代码。

class MpdfController extends Controller
{
    public function actionPersonalinfo() {
        // Your SQL query here
        $content = $this->renderPartial('personalinfo', ['model' => $model]);

        // setup kartik\mpdf\Pdf component
        $pdf = new Pdf([
            // set to use core fonts only
            'mode' => Pdf::MODE_CORE,
            // A4 paper format
            'format' => Pdf::FORMAT_A4,
            // portrait orientation
            'orientation' => Pdf::ORIENT_PORTRAIT,
            // stream to browser inline
            'destination' => Pdf::DEST_BROWSER,
            // your html content input
            'content' => $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:18px}',
            // set mPDF properties on the fly
            'options' => ['title' => 'ProfileStudent'],
            // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Profile'],
                'SetFooter'=>['{PAGENO}'],
            ]
        ]);

        /*------------------------------------*/
        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
        $headers = Yii::$app->response->headers;
        $headers->add('Content-Type', 'application/pdf');
        /*------------------------------------*/

        // return the pdf output as per the destination setting
        return $pdf->render();
    }
    ...
}

谢谢

【问题讨论】:

  • 您有错误吗? ..错误的结果?没有结果? ..有什么问题?
  • 没有结果 ..@scaisEdge

标签: yii yii2 yii2-advanced-app yii2-user


【解决方案1】:

好像你没有模型。尝试在调用此操作时传递 id 并使用 id 检索 $model 中的信息

public function actionPersonalinfo($id) {

  $model = $this->findModel($id);


  $content = $this->renderPartial('personalinfo', ['model' => $model]);

  // setup kartik\mpdf\Pdf component
  $pdf = new Pdf([
  // set to use core fonts only
  'mode' => Pdf::MODE_CORE,
  ....

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2016-06-15
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多