Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vannian's avatar

Donload pdf file from vue

I have the controller which returns a pdf:

    public function generatePDF(Request $request) {
     
        $pdf = PDF::setOptions(['isHtml5ParserEnabled' => true, 
                                'isRemoteEnabled' => true, 
                                'defaultFont' => 'sans-serif',
                                'chroot'  => public_path('storage/e-signatures')])
                    ->loadView('pdf.contract_pdf', compact('contract'));

        $pdf->setBasePath($_SERVER['DOCUMENT_ROOT']);
        $pdf->setPaper('A4', 'portrait');
	    return $pdf->output();                    
    }

and on the front end vue I have this:

<script setup>
    import axios from 'axios';

function onClick() {
            axios({
                  url: 'api/downloadPdf',
                  method: 'GET',
                  responseType: 'arraybuffer',
              }).then((response) => {
                   let blob = new Blob([response.data], {
                            type: 'application/pdf'
                        })
                        let link = document.createElement('a')
                        link.href = window.URL.createObjectURL(blob)
                        link.download = 'test.pdf'
                        link.click()
              });
          }
</script>

getting unexpected token

0 likes
2 replies
vybeauregard's avatar

I'm willing to bet there's more to that error message than just unexpected token. Tracking that down should reveal where the issue is.

Please or to participate in this conversation.