1

A dynamically generated files (According to user inputs) need to be downloaded...

I have been using mPDF (php module to create pdf files).

gen.php

require("./MPDF56/mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World</p>');
$mpdf->Output('filename.pdf','I');

This scripts works perfectly. Output method can have several options Like

'F' : to save pdf to local.
'S' : to return file as string.
'D' : to download the file.
'I' : force download... plug-in if available.

http://mpdf1.com/manual/index.php?tid=125

whenever a browser read the gen.php. pdf file will be downloaded. As i said it works perfectly ...

Until , i try to do this with ajax.

This script...

$.ajax({
  type:           'POST',
  cache:          false,
  url:            './gen.php',
  data:           JSON.stringify(inps),
  contentType:    'application/json',
});

triggers the gen.php. PDF file is created. However, file is not sent to the browser.

If i add this to the ajax part...

success: function(response){
  $("#result").html(response);
}

Document receives the file as a string. I tried all the options of the mPDF.OUTPUT function . no result.

I thought a work around but i dont know how to achive it.

Ajax need to open a pop-up while it sends the data. So File-download operation will be done under another tag.

Or maybe. I can try to open a invisible iframe of gen.php and send the data after i frame created...

Any suggestion will help.

SHORTLY

How can ajax call a php file (that downloads a file). After the necessary data is sent.

user2102266
  • 539
  • 3
  • 14
  • Possible duplicate: http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – bloodyKnuckles Jun 14 '14 at 18:39
  • This is highly over-qualified answer to my question. Let me clarify; Is it possible to open a new window after the ajax sent the data. – user2102266 Jun 14 '14 at 18:48
  • Ah, that helps. Short answer yes. But for the bigger picture, do you need to send information to the server first? I don't see it in your code. What is `inps`? – bloodyKnuckles Jun 14 '14 at 18:54
  • inps is a multi dimensional array (with key->val pairs) that contains all the inputs in a form. gen.php generate a pdf file using that array and send it to browser.(well it needs to that) – user2102266 Jun 14 '14 at 19:06
  • Can that go in a URL and execute `window.location='gen.php?key1=value1&key2=value2'`? Rather than use AJAX. – bloodyKnuckles Jun 14 '14 at 19:14
  • How big data can be sent via GET? – user2102266 Jun 14 '14 at 19:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55631/discussion-between-bloodyknuckles-and-user2102266). – bloodyKnuckles Jun 14 '14 at 19:21

1 Answers1

0

I think the question asked in the title of this post is answered here: Download a file by jQuery.Ajax

But there may be a simpler way to accomplish your goal, as I understand it. If you need to send information to the gen.php script and you're concerned about size of data sent to prepare the PDF and how an HTTP GET may handle that, use a FORM POST instead:

<form action="gen.php" method="post">
    <input type="text" name="X">
    <input type="text" name="Y">
    <input type="text" name="Z">
    <input type="submit" value="Download">
</form>

If mPDF responds with the appropriate headers, and I expect it does, the main page does not reload, but the file is sent to the browser for download.

Community
  • 1
  • 1
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
  • What i understand is. This form will be build via javascript (according to inps array) and posted to gen.php. So data will be sent via POST and gen.php will do the rest with traditional ways? . No ajax or any dynamic cross-side scrpits ? – user2102266 Jun 14 '14 at 21:30