Pdf 未调用yii2响应格式化程序

Pdf 未调用yii2响应格式化程序,pdf,yii2,responseformat,Pdf,Yii2,Responseformat,我正在尝试使用创建PDF,问题是,PDF类从来没有被称为格式响应类型 'response' => [ 'formatters' => [ 'html' => [ 'class' => 'yii\web\HtmlResponseFormatter', ], 'pdf' => [ 'class' => 'r

我正在尝试使用创建PDF,问题是,PDF类从来没有被称为格式响应类型

    'response' => [
        'formatters' => [
            'html' => [
                'class' => 'yii\web\HtmlResponseFormatter',
            ],
            'pdf' => [
                'class' => 'robregonm\pdf\PdfResponseFormatter',
                'mode' => '',
                'format' => 'A4',
                'defaultFontSize' => 0,
                'defaultFont' => '',
                'marginLeft' => 15,
                'marginRight' => 15,
                'marginTop' => 16,
                'marginBottom' => 16,
                'marginHeader' => 9,
                'marginFooter' => 9,
            ]
        ]
    ],
....
$file = $this->getPdf($report);
....
private function getPdf($report){
    Yii::$app->response->format = 'pdf';

   Yii::$container->set(Yii::$app->response->formatters['pdf']['class']);

    $this->layout = '//attachment';

    return $this->render('pdf/actionplan', ['model' => $report]);
}

$file变量中包含来自视图的HTML文本

您问错了问题。当然$file中有HTML,您正在从视图返回HTML

Yii::$app->response->format = 'pdf'; 
当控制器向浏览器返回响应时工作

您想问的是为什么控制器函数(我希望它在控制器中)不返回从HTML创建的PDF


如果你只想保存一个PDF,看看我是如何处理的:在actionPdf函数中。该函数返回一个文件,如果需要,可以保存在服务器上。

不幸的是,我无法使此扩展名正常工作。我确实干得很好

我建议改用kartik扩展