简体   繁体   中英

open pdf in new tab DOMPDF

After form submission, I am trying to open the pdf generated in a new tab. This is my code:

 public function invoice(Request $request) {
        $invoice = $this->invoice_form($request);

        $filename = 'pdfs/nir'.$invoice->invoice_id.'.pdf';

        $pdf = PDF::loadView('pdf-generation.invoice', compact('invoice'));

        $pdf->setPaper('A4', 'landscape');

        file_put_contents(''.$filename, $pdf->output());
        return $pdf->stream($filename, array("Attachment" => false));
    }

It works fine, but unfortunately it opens in the same tab. I want the pdf to open in a new tab. By the way, I also added target="_blank" in the form

I managed to do it like this. In my invoice() function I used Session:

Session::flash('fileToDownload', url($filename));

And then, in my header.blade.php I placed this:

@if(Session::has('fileToDownload'))
    <script>
        window.open("{{ Session::get('fileToDownload') }}", '_blank').focus();
    </script>
@endif

Hope it is going to help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM