Skip to content
Advertisement

Laravel download pdf returning random characters

Im trying to download my earlier saved pdf.

Ive created this function.

    {
        $storagePath = BillingStorage::findOrFail($billingTypeId);
        $filename= $storagePath->name;
        $headers = [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'attachment; filename=' . $filename,
        ];

        $filepath= storage_path().'/pdf/billing/invoice/'.$filename.'.pdf';

        return response()->download($filepath,$storagePath->name,$headers);

    }


And it returns this:

%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R >> /XObject << /I1 10 0 R /I2 11 0 R /I3 12 0 R /I4 13 0 R >> >> /MediaBox [
���mO�F���)F�
*�����j�� �����^����@����G��8v�<�Ȏ�;������=�sN����DHŸ����e�a��-�:�,���_'�1���
O �$�-�QoI�zߓt�t���:H�ޘ�`�|��;M1��G��ouեa�2��3e q���O�M�� ���iN� �dɸ"Q̚()� ��#�� ɸ"Q� ]1U
J`�zG:_v�.�ۤwӯ k����n�۪���yc�k�|�۠��5G6�� ;Iu�A �>A��������Z���� (!�g�

Var dumping my $filepath is resulting in this : C:laragonwwwYTBNstorage/pdf/billing/invoice/YTBN_Armando_3_2019-0305.pdf" Which is correct.

Advertisement

Answer

try to used like that

    $storagePath = BillingStorage::findOrFail($billingTypeId);

    $headers = [
        'Content-Type' => 'application/pdf',
    ];

    $filename = $storagePath->name.'.pdf'; //file name with extension add..
    $filepath = storage_path().'/pdf/billing/invoice/'.$filename;

    return response()->download($filepath,$filename,$headers);

or u can used directly in laravel 5.5 file path only no need to set header

    return response()->download($filepath);  
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement