dousi6192 2013-02-20 11:00
浏览 132
已采纳

保存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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?