douci6541
2018-02-14 10:23
浏览 114
已采纳

如何在yii2中的pdf标题上打印字符串

I am working on yii2 framework. I am printing a pdf file using kartik\mpdf\Pdf. There is a field named subdivision which I want to print on the header of the pdf file.

$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}']
        ]
    ]);

In above code, I want to do something like 'SetHeader' => ['Accurate (PVT) LTD.|Sub-Division: Here I want to print the sub division name|Generated On: ' . date("Y-m-d h:i:sa")],

What I have tried?

I have tried to do it by adding

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

and also

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

But Both are giving me non-object error. Is there any method to put a string on the header taken from a query ?

Any help would be highly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanaozhong0696 2018-02-14 12:31
    已采纳
    '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")]
    

    What are you trying to achieve with above lines of code, You are setting an array index SetHeader which expects its value to be a string, why php tags and why print_r when you just have to concatenate multiple strings. Read it like below.

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

    That being said, you can set header by two methods.

    Method 1

    Supply header in configuration array (thats the method you are trying), but you are not building header string in a correct way. It should be like following.

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

    Above configuration means "Call SetHeader method of mPDF class with supplied argument".

    Method 2

    Alternatively, you can obtain mPDF object and call its SetHeader method directly.

    $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"));
    
    点赞 评论

相关推荐 更多相似问题