Cs-Cart Pro 3.0.x: Sending Http Header And File To The Browser Inside The Post Controller


In the admin area (orders), I have a custom addon with a post controller orders.post.php. It is sending and receiving data between an external system using CURL. The exchange of the data itself is a success, but the problem arises with the code shown below when I need to present the results to the user:

<br />
if ($mode == 'generate_pdf_label') {<br />
.<br />
.<br />
some code omitted, the problematic part is shown below<br />
.<br />
.<br />
header('Content-type: application/pdf');<br />
header('Content-Disposition: attachment; filename="label.pdf"');<br />
readfile($pdf_download_URL); // this will push the file directly to the output buffer<br />
<br />
fn_set_notification('N', fn_get_lang_var('label_created_successfully'));<br />
return array(CONTROLLER_STATUS_REDIRECT, "orders.details?order_id=$_REQUEST[order_id]");<br />
}<br />

```<br />
<br />
The problem here is that the readfile() successfully returns the PDF file to the browser, but it also "exits" the code right after that, so the PHP never reaches the fn_set_notification()-function or the CONTROLLER_STATUS_REDIRECT.<br />
<br />
This is exactly what I am trying to achieve:<br />
1) In orders.details page I click link that will take me to the orders.generate_pdf_label (the post controller shown above)<br />
2) Some data is being exchanged between an external system<br />
3) External system returns me an external URL where the PDF label is stored at<br />
4) I need to push that PDF to the browser (the readfile()-function)<br />
5) Then I need to show a notification and "redirect" back to the orders.details page to get the page "refreshed"<br />
<br />
Any ideas on how to achieve this are greatly appreciated!<br />
<br />
Best regards,<br />

You can't do anything after outputing to the browser but to exit. You can do the fn_set_notification() before (which will make it available upon page refresh) and then you could do a redirect to the current page (or another). But you can't continue processing after output without doing some funky things with output buffering.

Thanks, understood. If I do not find out another way around this, then I will have a look at the output buffering options in PHP.

But there is something else. I tried to do a workaround for this, and assigned the “download PDF URL” to a templater so I could manually click the link and get the PDF out that way, but the variable in the templater stays blank. I have always managed to assign the variables to the templater without any problems, but not now with this case. Is it possible that there are some additional (hidden) redirects that wipe my template variable? Should I use pre controller insted of post?

The assigned variable from the post controller to the variable is a well formed URL, so could there be a security feature in Cs-Cart or something that prevents passing external URL's to the templater? Im not easily buying this idea though.

In cs-cart, every POST is followed by a GET. There are no page displays from POST requests.