2013-02-20 48 views
2

我想在用户单击按钮时生成并打开PDF。这是我到目前为止的代码:由TCPDF生成的保存文件

的index.php:

</html> 
<head> 
<script> 
$('.cmd_butt').click(function() { 
    $.ajax({ 
    url: 'create_pdf.php', 
    data: {id: $(this).attr('order_id'), name: $(this).attr('name')}, 
    type: 'post' 
    }); 
} 
</script> 
</head> 
<body> 
<button class="cmd_butt" order_id="250" name="pdf_but">Download PDF</button> 
</body> 
</html> 

create_pdf.php:

<?php 

//header('Content-type: application/pdf') 
header("Content-type: application-download"); 
//header("Content-Length: $size"); 
header("Content-Disposition: attachment; filename=MyPDF.pdf"); 
header("Content-Transfer-Encoding: binary"); 

require_once('tcpdf/config/lang/eng.php'); 
require_once('tcpdf/tcpdf.php'); 

// create new PDF document 
// some code 
// some code 

//$pdf->Output('test.pdf', 'I'); 
//$pdf->Output('test.pdf', 'FD'); 
$pdf->Output(); 

?> 

我怎样可以打开生成的PDF一个新的窗口?或通过“另存为”对话框通知用户下载此PDF文件?我尝试过不同的Output()组合,但它们都没有工作。

+0

[POST到服务器的可能重复,接收PDF,交付给用户w/jQuery](http:// stackov err3.com/questions/2186562/post-to-server-receive-pdf-deliver-to-user-w-jquery) – Adeel 2013-02-20 11:05:39

+0

不是重复。该帖未解释如何用PDF打开新窗口 – Alex 2013-02-20 11:09:20

+0

jQuery'.ajax'不会重定向,您可能需要使用'GET'方法或创建一个虚拟表单,然后提交 – 2013-02-20 11:19:48

回答

3

Ajax只能读取文本数据,不能直接显示PDF或在新窗口显示PDF(来自响应)。

您需要提交表单而不是AJAX,或者从ajax调用创建文件并作为响应,获取关于文件创建的响应。例如创建文件名,并打开一个新的窗口基于此。

+0

在这种情况下,如何在用户下载PDF文件后处理PDF文件? – Alex 2013-02-20 11:29:27

+0

只需提交表格并使用Basant提及的方法。在这种情况下,该文件不是在File系统上创建的,因此不需要担心处置。 – Adeel 2013-02-20 12:09:04

4

你有没有试过这种

$pdf->Output('yourfilename.pdf','D'); 

这将提示用户选择在哪里保存PDF文件。

+1

这种方法也不管用.. – Alex 2013-02-20 11:16:20

+0

谢谢你太多了!我不想保存到服务器。我已经有一个页面生成使用TCPDF的PDF ...但在我的脚本结束时,我使用'我'而不是'D'。 'D'的简单改变奏效了! – Kirt 2013-09-18 15:17:31

+0

@Webgrity这引发错误'TCPDF错误:无法创建输出文件:../ invoice_job_6.pdf' – 2015-01-09 12:19:15

0

变化

<button class="cmd_butt" order_id="250" name="pdf_but">Download PDF</button> 

<a href="create_pdf.php?order_id=250" target="_blank">Download PDF</a> 

删除在的index.php
在Java中create_pdf.php取下头

相关问题