我正在研究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
错误。是否有任何方法将字符串放在从查询中获取的标题上?
任何帮助都将受到高度赞赏。
答案 0 :(得分: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']
话虽这么说,你可以通过两种方法设置标题。
方法1
在配置数组中提供标头(这就是您正在尝试的方法),但您没有以正确的方式构建标头字符串。应该如下。
'SetHeader' => ['Accurate (PVT) LTD.|Sub-Division: '.$dataProvider->getModels()[0]['Sub_Division'].'|Generated On: ' . date("Y-m-d h:i:sa")]
以上配置意味着&#34;使用提供的参数&#34;调用SetHeader
类的mPDF
方法。
方法2
或者,您可以获取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"));