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

dineshsuthar92's avatar

Unable to stream download a base64 encoded pdf file from mongo db

I am using laravel 5.7 and mongo db for my application. I am storing a pdf file in base64 encoded string in mongodb. When I try to download the file it gives me exception as below:

LogicException
The content cannot be set on a StreamedResponse instance.

In my controller method to download I am using this code:

return response()->streamDownload(function () use ($message) {
            echo base64_decode(substr($message->content['data'], 28));
        }, $message->content['file_name']);

where message is the object returned from mongodb. Example:

{
    "_id" : ObjectId("5caadea9c4c1a6134b3a7d13"),
    "user_id" : 8,
    "trade_id" : 4,
    "type" : 3,
    "content" : {
        "data" : "data:application/pdf;base64,JVBER..."
        "file_name": "test.pdf"
        }
}

Expected result: I should be able to download the pdf file without storing physically on the server disk.

0 likes
0 replies

Please or to participate in this conversation.