php 使用 AJAX 和 Jquery 使用 FPDF 创建 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13209897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using AJAX and Jquery to create pdf using FPDF
提问by calvin12
I am trying to create pdf file using FPDF.
我正在尝试使用 FPDF 创建 pdf 文件。
What I right now have is an HTML page with various lines of data and a print button next to it. When someone clicks Print, I am sending corresponding data using Jquery by making AJAX call..
我现在拥有的是一个 HTML 页面,其中包含多行数据和旁边的打印按钮。当有人点击打印时,我通过进行 AJAX 调用使用 Jquery 发送相应的数据..
This is my JQUERY code:
这是我的 JQUERY 代码:
$('.printbtn').live('click', function(){
var printdata = 'name=' + name_t + '&address=' + address_t;
$.post('print.php', printdata, function(){
});
return false;
});
this is print.php
这是print.php
$name = $_POST['name'];
$address = $_POST['address'];
require ("fpdf17/fpdf.php");
$pdf = new FPDF('P','mm',array(90,100));
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,10,'name: '.$name);
$pdf->Cell(0,10,'address: '.$address);
$pdf->Output();
?>
But I am not getting PDF in return. Whats wrong?? In fact, nothings happening.. I want to retreive the pdf file and send it to print
但我没有得到 PDF 作为回报。怎么了??事实上,什么也没发生..我想检索pdf文件并将其发送打印
采纳答案by Nelson
To load a PDF via ajax you could try:
要通过 ajax 加载 PDF,您可以尝试:
Use some of the PDF javascript libraries to render PDF directly from javascript without plugins, like mozilla pdf.jsor jspdf. And try to see if those libraries let you set the pdf directly from binary data (what you receive in ajax).
Another option is receive the pdf data as base64 encoded and the use a Data URIto set
window.location.hrefto that data uri, although in that case it's possible the PDF is offered as a dowload dialog instead of loading directly in the page, you have to test that. Also data uri support is very limited for pdf in IE 8 and older, see the wikipedia data uri pagefor more details.Also see this answerabout how it's not possible to load pdf directly from ajax and what other options you have to try to do what you want (mainly saving pdf as temporary file on server and use it with
window.location.hrefor<iframe>orwindow.open)
使用一些 PDF javascript 库直接从 javascript 渲染 PDF,无需插件,如mozilla pdf.js或jspdf。并尝试查看这些库是否允许您直接从二进制数据(您在 ajax 中收到的数据)设置 pdf。
另一种选择是接收 pdf 数据作为 base64 编码并使用数据 URI设置
window.location.href为该数据 uri,尽管在这种情况下,PDF 可能作为下载对话框提供,而不是直接加载到页面中,您必须测试. 此外,IE 8 及更早版本中 pdf 的数据 uri 支持非常有限,请参阅维基百科数据 uri 页面以获取更多详细信息。另请参阅有关如何无法直接从 ajax 加载 pdf 以及您必须尝试执行所需操作的其他选项(主要是将 pdf 保存为服务器上的临时文件并与or或一起使用)的答案
window.location.href<iframe>window.open
回答by sdespont
You should open a new page using <a>tag with your PHP page print.phpwith your variables.
您应该使用带有变量的<a>PHP 页面的标签打开一个新页面print.php。
<a href="print.php?data" target="_blank">click me to download the file</a>
<a href="print.php?data" target="_blank">click me to download the file</a>
In the PHP page, add headers
在 PHP 页面中,添加标题
// Send Headers
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="myPDF.pdf');
// Send Headers: Prevent Caching of File
header('Cache-Control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
Try to play with header header('Content-type: application/force-download');to download automatically your file.
尝试使用标题header('Content-type: application/force-download');自动下载您的文件。
You could also display your PDF data like if the file has been saved readfile('original.pdf');
您还可以显示您的 PDF 数据,就像文件已保存一样 readfile('original.pdf');

