dousi6192
2013-02-20 11:00
浏览 117

保存TCPDF生成的文件

I'd like to generate and open PDF when user clicks on a button. Here is my code so far:

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();

?>

How can I open a new window with generated PDF? Or notify user with "save as" dialog to download this PDF? I've tried different Output() combinations but none of them worked.

图片转代码服务由CSDN问答提供 功能建议

我想在用户点击按钮时生成并打开PDF。 这是我到目前为止的代码:

index.php:

 &lt; / html&gt; 
&lt; head&gt; 
&lt; script&gt  ; 
 $('。cmd_butt')。click(function(){
 $ .ajax({
 url:'create_pdf.php',
 data:{id:$(this).attr('order_id  '),name:$(this).attr('name')},
 type:'post'
}); 
} 
&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt;  
&lt; button class =“cmd_butt”order_id =“250”name =“pdf_but”&gt;下载PDF&lt; / button&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   \  n 
 

create_pdf.php:

 &lt;?php 
 
 // header('Content-type:application / pdf')
header(“ 内容类型: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'); 
 
 //创建新的PDF文档
  //某些代码
 //某些代码
 
 // $ pdf-&gt;输出('test.pdf','I'); 
 // $ pdf-&gt;输出('test.pdf'  ,'FD'); 
 $ pdf-&gt;输出(); 
 
?&gt; 
   
 
 

如何使用生成的PDF打开新窗口? 或者通过“另存为”对话框通知用户下载此PDF? 我尝试了不同的Output()组合,但没有一个工作。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtjxhf595733 2013-02-20 11:22
    已采纳

    Ajax can only fetch textual data, it can't show the PDF directly or in a new window (from response).

    You need to either submit form instead of AJAX, or create a file from ajax call and in response, gets a response about creation of file. e.g. created file name, and than open a new window based on that.

    打赏 评论
  • donglu9978 2013-02-20 11:10

    Have you tried this

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

    This will prompt the user to choose where to save the PDF file.

    打赏 评论
  • dongyimo1293 2013-03-28 10:47

    Change

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

    to

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

    Remove the java in index.php
    Remove the headers in create_pdf.php

    打赏 评论

相关推荐 更多相似问题