Ajax 帖子创建 pdf
我正在使用 ajax 发布到通过 TCPDF 创建 PDF 文档的函数。
通常,我只会对该函数执行正常的发布操作,然后输出 PDF,允许用户下载 pdf 文件。然而,我的理解是,这不适用于 ajax,而是需要将 pdf 文件保存在服务器上,然后将文件的 url 返回给 ajax 调用。
一旦我有了网址,我就可以做类似
window.location.assign(url/to/my.pdf);
“好的”这样的事情,所以这一切都很好,但效果不是很好。首先,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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Apache 作为 Web 服务器,则可以将以下内容添加到生成 PDF 文件的文件夹中的
.htaccess
文件中以强制下载。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.您可以通过 PHP 传递生成的 PDF 并设置附加标头以强制浏览器下载该文档。请参阅 php 强制下载扩展。
我想您也可以让 Apache 自动设置这些标头(例如,对于特定文件夹中的 PDF)。
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).