sbardascione's avatar

Force download a pdf file from a base64 string

Hello everybody, i'm triyng to force download a pdf file that is stored in a base64 string in my database.

I'm using a link and it doesn't download the file, it opens a page with the pdf.

First of all what should i use in your opinion? A button, a link.. something else? Here is the method i use to output my file

public function getPdf(Request $request)
    {
        $pdf_id = Input::get('pdf_id');
        $result = DB::table('pdf_contents')
            ->select('pdf')
            ->where('id',(int)$pdf_id)->first();
        $base_64_pdf = $result->pdf;
        $pdf = base64_decode($base_64_pdf);

        return (new Response($pdf,200))->header('ContentType','application/pdf');
    }

Thanks in advance

0 likes
4 replies
sbardascione's avatar

I don't have a file in my file system so i guess i cannot use your solution, thanks however.

ohffs's avatar

Can you add an extra header for 'Content-Disposition: attachment; filename="downloaded.pdf"' and see if that helps? Trying to over-ride what a browser wants to do (or what a user has configured it to do) can be tricky. Worst case you could write the decoded data to a temporary file and try response()->download(...) and see if that helps.

Please or to participate in this conversation.