A
A
AlexSer2018-10-02 15:42:59
Yii
AlexSer, 2018-10-02 15:42:59

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.
5bb366ea9b991233094834.png
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();

    }

when you click generate, an ajax request is sent
$('#sformirovat').click(function() {
        $.ajax({
                 url:'/list/statdata',
                 type:"POST",
                 data:{
         
                     },
                 success:function(data) {
                console.log(data);
                }
            });
});

I know how to open it in a new window, but I don’t know how to force it to download after a request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Karavaev, 2018-10-02
@AlexSer

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 question

Ask a Question

731 491 924 answers to any question