I am writing a PHP file that receives post data (via a React Native App) and create a PDF file using mPDF. At the end of that file I want to be able to download the file, or see it in the browser or upload it on the server. In addition to that I do a json_encode($array)
to be able to retrieve the name of the file and its path on the server.
$result = array('generatedPDF' => $file,'path' => $path);
echo json_encode($result);
$mpdf->Output('report.pdf','D'); // For Download
// $mpdf->Output($filename,'F'); // For Upload
// $mpdf->Output(); // For Browser
return json_encode($result);
The download and the display on the browser both work (I have an error 500 for the upload...) but in the debugging tool, I have nothing in the response (in Network). If I delete the Output lines, I have the a response
{"generatedPDF":"report.pdf","path":"\/folder\/folder\/report.pdf"}
Is it possible to have both a working Output method and a response ?
An HTTP request can return only one resource. So it's impossible to return both a PDF and a JSON at the same time.
You should get the JSON first and issue a second request to retrieve the PDF.