2016-05-26 3 views
1

yii2コン​​ソールでpdfを生成したいがエラーが発生する。コードはwebでは正しく実行されますが、consoleでは正しく実行されません。私はmdfの拡張子を使用しています。yii2コン​​ソールでpdfを生成

public function actionInvoicePdf($invoice) { 
    $content = $this->renderPartial('_invoicePdf',['invoice'=>$invoice]); 
    Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; 
    $headers = Yii::$app->response->headers; 
    $headers->add('Content-Type', 'application/pdf'); 
    $path = Yii::getAlias('@uploadPath').'/pdf/commission-invoice/'; 
    // setup kartik\mpdf\Pdf component 
    $pdf = new Pdf([ 
     'mode' => Pdf::MODE_CORE, 
     'format' => Pdf::FORMAT_A4, 
     'filename' => 'test.pdf', //$path.$invoice->invoice_filename, 
     'orientation' => Pdf::ORIENT_PORTRAIT, 
     'destination' => Pdf::DEST_FILE, //Pdf::DEST_FILE 
     'content' => $content, 
     'cssInline' => '.kv-heading-1{font-size:18px}', 
     // set mPDF properties on the fly 
     'options' => ['title' => 'Invoice #'], 
     // call mPDF methods on the fly 
     'methods' => [ 
      'SetHeader'=>['Invoice:buyold'], 
      'SetFooter'=>['{PAGENO}'], 
     ] 
    ]); 
    return $pdf->render(); 
} 

ここでエラー例外

'yii\base\UnknownPropertyException' with message 'Setting unknown prop 
erty: yii\console\Response::format' 

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown prop 
erty: yii\console\Response::headers' 

を取得する私のコンソールの設定ファイルは]

return [ 
'id' => 'app-console', 
'basePath' => dirname(__DIR__), 
'bootstrap' => ['log'], 
'controllerNamespace' => 'console\controllers', 
'components' => [ 
    'log' => [ 
     'targets' => [ 
      [ 
       'class' => 'yii\log\FileTarget', 
       'levels' => ['error', 'warning'], 
      ], 
     ], 
    ], 
    'session' => [ // for use session in console application 
     'class' => 'yii\web\Session' 
    ], 
], 

'params' => $params, 

です。

+0

一度コンソールの設定ファイルを確認してください。 Ref:http://stackoverflow.com/questions/34174750/yii2-getting-unknown-property-yii-console-applicationuser –

+0

OK私の質問を更新します。これで私のコンソール設定ファイル –

答えて

0

コンソールアプリケーションのデフォルトの応答クラスは、yii\console\Responseではなく、yii\web\Responseです。そのように:コンソールアプリケーションは、プレーンテキストを返す必要があり、コールがウェブサーバへのコマンドライン・インタフェースを介してではないので、

  • が何のヘッダがないので、

    1. 何のフォーマットはありません。

    は、したがって、次の行を削除する必要があります。

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

    が間違っています。私はそれを保存する必要があるので、ヘッダー形式は必要ありません:-) –