Ajax post to create pdf
I am using ajax to post to a function that creates a PDF document through TCPDF.
Normally, I would just do a normal post to the function, and that would output the PDF, allowing the开发者_StackOverflow user to download teh pdf file. My understanding, however, is that this doesnt work with ajax, and that I instead need to save the pdf file on the server, and then return the url of the file to the ajax call.
Once I have the url, then I can do something like
window.location.assign(url/to/my.pdf);
Ok, so this all works fine, but its not great. Firstly, the pdf doesnt open in a new window (i.e. it currently opens in the same window), and secondly, I'd prefer to force the user to download the file rather than it opening in the browser.
Are there any other alternatives?
If you're using Apache for your web server, then you can add the following to an .htaccess
file in the folder where your PDF files are generated to force a download.
<Files *.pdf>
ForceType application/pdf
Header set Content-Disposition attachment
</Files>
You could pass the generated PDF through PHP and set additional headers to force browser to download the document. See php force download extension.
I guess you can make these headers to be set by Apache automatically as well (e.g. for PDFs in particular folder).
精彩评论