Answer the question
In order to leave comments, you need to log in
How to get PDF from Ajax request in yii2?
Hey! Tell me how to make the generated PDF document not open in the browser, but immediately downloaded in Yii2.
After clicking Generate, you need to download the PDF document in the browser.
public function actionStatdata(){
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
'format' => Pdf::FORMAT_A4,
'content' => $this->renderPartial('statdata'),
'options' => [
'title' => 'Privacy Policy - Krajee.com',
'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
],
'methods' => [
'SetHeader' => ['Создано ' . date('d.m.Y H:s:i')],
'SetFooter' => [''],
]
]);
return $pdf->render();
}
$('#sformirovat').click(function() {
$.ajax({
url:'/list/statdata',
type:"POST",
data:{
},
success:function(data) {
console.log(data);
}
});
});
Answer the question
In order to leave comments, you need to log in
There is a wonderful function in Response:
Well, the whole family of similar functions, such as: sendStreamAsFile()
You can, of course, try to configure the PDF itself to render in php://output and it will be sent, but perhaps without the correct headers. This needs to be clarified in the library that you are using.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question