AJAX调用后发送PDF浏览器 [英] send pdf to browser after ajax call

查看:96
本文介绍了AJAX调用后发送PDF浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被通过Ajax调用称为PHP脚本。值发送到这个脚本建立一个PDF格式。我想将PDF发送到浏览器,但自认为构建PDF返回页面的JavaScript脚本我看不出如何做到这一点。任何想法?

解决方案

我会推荐一些有点不同。相反AJAX调用进行重定向到一个这样的网址:

  ./ path_to_pdf_script /运行s​​cript.php参数1 = VAL1和放大器;参数2 = val2的
 

该脚本将其产生的PDF之一。广场上的脚本,这个头顶部的地方:

 标题(内容类型:应用程序/ PDF格式);
 

和简单的回声字符串的PDF内容。如果你希望用户下载观看,你可以做的AJAX调用中发现的例子本PDF代替< A HREF =htt​​p://php.net/manual/en/function.header.php相对=nofollow>这里:

  

从php.net

     

如果您想提示用户保存所发送的数据,   如生成的PDF文件,可以使用»内容处置   头提供一个推荐的文件名,并在浏览器强制   显示保存对话框。

 &LT; PHP
//我们将输出PDF
标题(内容类型:应用程序/ PDF格式);

//它将被称为downloaded.pdf
标题(内容处置:附件;文件名=downloaded.pdf');

// PDF格式的来源是original.pdf
ReadFile的('original.pdf');
?&GT;
 

I have a php script that gets called via an ajax call. Values are sent to this script to build a pdf. I want to send the pdf to the browser, but since the script that builds the pdf returns to the page with the javascript I can't see how to do this. Any ideas?

解决方案

I would recommend something a bit different. Instead of AJAX call make a redirect to an URL like this:

./path_to_pdf_script/script.php?param1=val1&param2=val2

This script would be the one which generated the pdf. Place somewhere on top of the script this header:

header('Content-type: application/pdf');

And simply echo the string the pdf content is in. If you want the user to download this pdf instead of viewing you could do the AJAX call with the example found 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');
?>

这篇关于AJAX调用后发送PDF浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆