How to download PDF File Generated from TCPDF (PHP) using AJAX (jQuery)?

后端 未结 4 1414
粉色の甜心
粉色の甜心 2021-01-18 09:33

I am using Yii Framework, TCPDF and jQuery to generate a pdf.

The pdf is generated by inputing in a form and submitting it using ajax.

The pdf is created but

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 09:45

    If the problem is that you are not getting the browser's download dialog for the PDF, then the solution is to do it this way:

    First, redirect the browser (using window.location as the other answers say) to navigate to a special controller action in your application, e.g. with this url: http://your.application.com/download/pdf/filename.pdf.

    Implement the action referenced in the URL like this:

    public function actionPdf() {
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename="filename.pdf";');
        header('Content-Length: '.filesize('path/to/pdf'));
        readfile('path/to/pdf');
        Yii::app()->end();
    }
    

    This will cause the browser to download the file.

提交回复
热议问题