0

我正在研究yii2框架。我正在pdf使用kartik\mpdf\Pdf. 有一个名为subdivision我想在pdf文件头上打印的字段。

$sql_r = "SELECT DISTINCT ogph.`id` AS 'OGP_Serial_No',DATE(ogph.`created_at`) AS 'Date',uu.`name` AS 'Created_By',ogpd.`meter_serial` AS 'Meter_Serial_Number',  u.`name` AS 'Issued_To', ps.`name` AS 'Store',sd.`name` AS 'Sub_Division',
IFNULL(ogph.`admin_incharge`,'') AS 'Admin_Incharge', IFNULL(ogph.`project_manager`,'') AS 'Project_Manager', IFNULL(ogph.`driver_name`,'') AS 'Driver_Name', IFNULL(ogpd.`remarks`,'') AS 'Remarks'  
FROM `ogp_header` ogph
INNER JOIN `ogp_detail` ogpd ON ogph.`id` = ogpd.`ogp_id` 
INNER JOIN `user` u ON ogph.`issuer` = u.`id`  
INNER JOIN `user` uu ON ogph.`created_by` = uu.`id`
INNER JOIN `survey_hesco_subdivision` sd ON ogph.`sub_division` = sd.`sub_div_code`
INNER JOIN `project_store` ps ON ogph.`store_id` = ps.`id` $where AND ogph.`id` = $ogp_id";
        $sql_r = $sql_r . " LIMIT " . $from . " OFFSET " . $to;

        $dataProvider = new SqlDataProvider([
            'sql' => $sql_r,
            'pagination' => false
        ]);


    $content = $this->renderPartial('view_pdf_ogp', ['dataProvider' => $dataProvider]);

    $pdf = new Pdf([
        // set to use core fonts only
        'mode' => Pdf::MODE_UTF8,
        // A4 papr format
        'format' => Pdf::FORMAT_A4,
        // portrait orientation
        'orientation' => Pdf::ORIENT_PORTRAIT,
        // stream to browser inline
        'destination' => Pdf::DEST_DOWNLOAD,
        // 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:14px}',
        // set mPDF properties on the fly
        'options' => ['title' => 'OGP Document'],
        // call mPDF methods on the fly
        'methods' => [
            'SetHeader' => ['Name.||Generated On: ' . date("Y-m-d h:i:sa")],
            'SetFooter' => ['{PAGENO}']
        ]
    ]);

在上面的代码中,我想做类似的事情'SetHeader' => ['Accurate (PVT) LTD.|Sub-Division: Here I want to print the sub division name|Generated On: ' . date("Y-m-d h:i:sa")],

我试过什么?

我试图通过添加来做到这一点

'SetHeader' => ['Name.|Sub-Division: '.<?php $model=$dataProvider->getModels()[0]; print_r($model['Sub_Division'])?>.'|Generated On: ' . date("Y-m-d h:i:sa")]

并且

'SetHeader' => ['Name.|Sub-Division: '.$content->Sub_Division.'|Generated On: ' . date("Y-m-d h:i:sa")],

但是两者都给了我non-object错误。有什么方法可以将字符串放在从查询中获取的标题上?

任何帮助将不胜感激。

4

1 回答 1

1
'SetHeader' => ['Accurate (PVT) LTD.|Sub-Division: '.<?php $model=$dataProvider->getModels()[0]; print_r($model['Sub_Division'])?>.'|Generated On: ' . date("Y-m-d h:i:sa")]

你想用上面的代码行来实现什么,你正在设置一个数组索引SetHeader,它期望它的值是一个字符串,为什么是 php 标签以及为什么print_r你只需要连接多个字符串。像下面这样阅读。

'SetHeader' => ['header string goes here']

话虽如此,您可以通过两种方法设置标题。

方法一

在配置数组中提供标题(这就是您正在尝试的方法),但您没有以正确的方式构建标题字符串。它应该像下面这样。

'SetHeader' => ['Accurate (PVT) LTD.|Sub-Division: '.$dataProvider->getModels()[0]['Sub_Division'].'|Generated On: ' . date("Y-m-d h:i:sa")]

以上配置表示“使用提供的参数调用类的SetHeader方法”。mPDF

方法二

或者,您可以获取mPDF对象并直接调用其SetHeader方法。

$mpdf = $pdf->api; // fetches mpdf api
$mpdf->SetHeader('Accurate (PVT) LTD.|Sub-Division: '.$dataProvider->getModels()[0]['Sub_Division'].'|Generated On: ' . date("Y-m-d h:i:sa"));
于 2018-02-14T12:31:01.763 回答