I'm using Laravel as an API and interacting with it via React. I am trying to serve PDF files to the client, which seems to work, but the downloaded files are always coming up empty. I've tried to do numerous things with base64 encode/decode, utf8 encode/decodes, multiple ways of serving the files from the server, but ultimately the file either comes up blank (but the file opens without any issues) or the file is corrupt once downloaded. My current code (which generates empty PDf files) is as follows:
PHP
public function servePDF($statement_id) {
$statement = Statement::where('id', $statement_id)->first();
if($statement === null){
return response()->json(['message' => "Statement does not exist.", "success" => false], 404);
}
$full_path = $this->pdf_path.$statement->filename;
$storage_path = storage_path('app/generated/'.$full_path);
//NOTE: Worth noting that both of these return statements will lead
//to the same response content, headers and behaviour (empty file)
//return \Storage::disk('generated')->get($full_path);
return response()->download($storage_path);
}
I know for sure that the correct file is being served which has content, and the file exists, because if i give it an obviously invalid path I get errors thrown by the server -- so I am sure that the path, and contents of the file are 100% correct.
Client: (just the request)
download: (resource, params) => {
return httpClient(`${apiUrl}/${resource}/download/${params.id}`, {
method: 'GET'
}).then((res) => {
console.log(res);
return {
'data': new Blob([res.body], {
type: 'application/pdf'
})
}
});
}
Request handling / downloading:
const download = e => {
e.stopPropagation();
dataProvider
.download(resource, {id: record.id})
.then(res => {
console.log("handling request");
console.log(res);
const href = window.URL.createObjectURL(res.data);
const a = linkRef.current;
a.download = 'Test.pdf';
a.href = href;
a.click();
a.href = '';
})
.catch(error => notify(error.message, 'warning'));
}
The console.log(req) in the initial request outputs the following... The symbols tell me there is something wrong with the encoding but im absolutely not sure how to solve it.
Object { status: 200, headers: Headers, body: "%PDF-1.7\n1 0 obj\n<< /Type /Catalog\n/Outlines 2 0 R\n/Pages 3 0 R >>\nendobj\n2 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj\n3 0 obj\n<< /Type /Pages\n/Kids [6 0 R\n]\n/Count 1\n/Resources <<\n/ProcSet 4 0 R\n/Font << \n/F1 8 0 R\n/F2 9 0 R\n>>\n>>\n/MediaBox [0.000 0.000 595.280 841.890]\n >>\nendobj\n4 0 obj\n[/PDF /Text ]\nendobj\n5 0 obj\n<<\n/Producer (��\u0000d\u0000o\u0000m\u0000p\u0000d\u0000f\u0000 \u00001\u0000.\u00000\u0000.\u00002\u0000 \u0000+\u0000 \u0000C\u0000P\u0000D\u0000F)\n/CreationDate (D:20210521135915+00'00')\n/ModDate (D:20210521135915+00'00')\n/Title (��\u0000S\u0000t\u0000a\u0000t\u0000e\u0000m\u0000e\u0000n\u0000t)\n>>\nendobj\n6 0 obj\n<< /Type /Page\n/MediaBox [0.000 0.000 595.280 841.890]\n/Parent 3 0 R\n/Contents 7 0 R\n>>\nendobj\n7 0 obj\n<< /Filter /FlateDecode\n/Length 244 >>\nstream\nx�u��N�0\u0014��>�\u001da����oG�\u0016��.�\u0010C�8(\u0012��$B�\u000f/JR!\u0014������s|w\u0002�\u0010�~���c�R� \u0003�)T�Ch��D@\u0012\u0005�\u0016���4�\u001e�j�{�BR!t!��\r�3\u001c�ڠ\u001dJ*\u0019C�����Eo\u007f\u001bH ��\u001f��1�qd �=�wL�!�U���o=��P+�+ɠ�\u0014�ҶP�qZ\u0004�eP\u0006�3���X�n�dPib��S��� �Y4V0YO1�a���>sWG�\u001a��ڋ��׆P���M\n��\u0019�%N�������\u001f�+��\nendstream\nendobj\n8 0 obj\n<< /Type /Font\n/Subtype /Type1\n/Name /F1\n/BaseFont /Times-Roman\n/Encoding /WinAnsiEncoding\n>>\nendobj\n9 0 obj\n<< /Type /Font\n/Subtype /Type1\n/Name /F2\n/BaseFont /Times-Bold\n/Encoding /WinAnsiEncoding\n>>\nendobj\nxref\n0 10\n0000000000 65535 f \n0000000009 00000 n \n0000000074 00000 n \n0000000120 00000 n \n0000000284 00000 n \n0000000313 00000 n \n0000000492 00000 n \n0000000595 00000 n \n0000000911 00000 n \n0000001020 00000 n \ntrailer\n<<\n/Size 10\n/Root 1 0 R\n/Info 5 0 R\n/ID[<99ebea0e5ffcc7617764df03428291a9><99ebea0e5ffcc7617764df03428291a9>]\n>>\nstartxref\n1128\n%%EOF\n", json: undefined }
Does anyone have any idea where i am going wrong and how I can fix it?