我有一个通过 ajax 调用调用的 php 脚本。值被发送到此脚本以构建 pdf。我想将 pdf 发送到浏览器,但由于构建 pdf 的脚本返回到带有 javascript 的页面,我看不到如何执行此操作。有什么想法吗?
最佳答案
我会推荐一些不同的东西。而不是 AJAX 调用,而是像这样重定向到 URL:
./path_to_pdf_script/script.php?param1=val1¶m2=val2
此脚本将是生成 pdf 的脚本。将此 header 放在脚本顶部的某处:
header('Content-type: application/pdf');
只需echo
pdf 内容所在的字符串。如果您希望用户下载此pdf 而不是查看您可以使用找到的示例进行AJAX 调用HERE :
from php.net
If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.
<?php // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?>
关于php - ajax 调用后将 pdf 发送到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859738/