0

My php file: example_061.php create PDF return and everything works fine.

Now I want to call that file with jQuery AJAX to get this at my screen and I try:

$("#proba").click(function() {
  //in here we can do the ajax after validating the field isn't empty.
  $.ajax({
    url: "tcpdf/examples/example_061.php",
    type: "POST",
    async: true, 
    data: { 
      ime: $("#ime").val(),
      pozicija: $("#pozicija").val(),
      jmbg: $("#jmbg").val()
    }, //your form data to post goes here as a json object
    dataType: "html",
    success: function(response) {
      window.open('example_061.pdf');
    },  
  });        
});

Everything is fine so success function work an I get alert message but I don't get PDF file as download or at screen. How I can do that?

4
  • at the end of PHP file I have: //Close and output PDF document return $pdf->Output('example_061.pdf', 'I'); Commented May 4, 2015 at 14:02
  • There are plenty of Ajax-like downloaders out there... Here is my very own for example: github.com/gasparesganga/jquery-ajax-downloader Commented May 4, 2015 at 14:03
  • Which PDF library are you using? What options do you have for ->Output()? Commented May 4, 2015 at 14:11
  • I update my question... so I send data to php file to get pdf output put how I can download now PDF with generated data that I send with ajax ? Commented May 4, 2015 at 14:14

1 Answer 1

2

You do not need to use AJAX to download a file. If the content-disposition header is set, it will be downloaded and the current page will not be reloaded.

Just add this to your php script that creates the PDF:

header('Content-Disposition: attachment; filename=' . $MyFileName); 

And instead of AJAX, use a regular anchor tag link:

<a href="tcpdf/examples/example_061.php">Download</a>

As someone noted in the comments, if you need to post you can still use a simple form with the same effect:

<form method="POST" ACTION="tcpdf/examples/example_061.php">
<input type="hidden" name="myPostItem" value="My POSt VALUE" />
<input type="submit" value="download">
</form>
Sign up to request clarification or add additional context in comments.

8 Comments

sorry but I need to send some data to php file to create pdf I need... so I need to use ajax...
@milemilicmile Why can't you use GET instead of POST?
@milemilicmile, you can still do a regular POST to example_061.php, even though it eventually outputs a PDF. No need to use AJAX for that...
I update my question, so I send data to php file to get pdf output put how I can download now PDF with generated data that I send with ajax ?
@milemilicmile You can't! Downloading something involves copying a file to the filesystem. JS does NOT have access to the filesystem, only the browser itself does. If you need to post, you need to use some variation of the answers posted above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.