Hi there,
I want to generate a pdf of the selected rows of a grid view like this:
JS of view:
<?php JSRegister::begin(); ?>
<script>
(function () {
document.getElementById('pdf').addEventListener('click', pdfSelectRows, true)
function pdfSelectRows() {
// javascript to call for your button on
// the view - which on click - will process the
// action for all selected rows
var keys = $('#grid').yiiGridView('getSelectedRows');
$.post({
url: 'notice/report', // your controller action
dataType: 'json',
data: {keylist: keys},
success: function(data) {
alert('I did it! Processed checked rows.')
},
});
}
})();
</script>
<?php JSRegister::end(); ?>
…and Controller:
public function actionReport() {
$this->layout = false;
//var_dump($ids);
// get your HTML raw content without any layouts or scripts
//$content = $this->renderPartial('_reportView');
$content = "";
foreach($_POST['keylist'] as $id)
{
$content = $content . '<br> ' . $this->render('_reportView', ['model' => $this->findModel($id),]);
}
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// 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:18px}',
// set mPDF properties on the fly
'options' => ['title' => ''],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>[''],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
The controller already get the selected keys and a pdf will be generated. But if I press my generate button in the grid view, no visible action (like a new site or pdf open) appears. With the object inspector I can the see the result. What code do I need to make the browser open the new generated pdf document?
Many thanks!
Toby