在 yii2 控制台生成 pdf
Generate pdf in yii2 console
我想在 yii2 控制台中生成 pdf 但出现错误。代码在 web
中正确执行,但在 console
中不正确。我正在为它使用 mdf
扩展。
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,
];
控制台应用程序的默认响应 class 是 yii\console\Response
not yii\web\Response
。因此:
- 没有格式,因为控制台应用程序应该 return 纯文本和
- 没有 headers 因为调用是通过命令行界面而不是网络服务器。
因此应删除以下行:
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$headers = Yii::$app->response->headers;
$headers->add('Content-Type', 'application/pdf');
我想在 yii2 控制台中生成 pdf 但出现错误。代码在 web
中正确执行,但在 console
中不正确。我正在为它使用 mdf
扩展。
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,
];
控制台应用程序的默认响应 class 是 yii\console\Response
not yii\web\Response
。因此:
- 没有格式,因为控制台应用程序应该 return 纯文本和
- 没有 headers 因为调用是通过命令行界面而不是网络服务器。
因此应删除以下行:
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$headers = Yii::$app->response->headers;
$headers->add('Content-Type', 'application/pdf');