【问题标题】:saving file generated by TCPDF保存由 TCPDF 生成的文件
【发布时间】:2013-02-05 08:33:02
【问题描述】:

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

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() 组合,但都没有奏效。

【问题讨论】:

标签: php jquery tcpdf


【解决方案1】:

你试过了吗

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

这将提示用户选择保存 PDF 文件的位置。

【讨论】:

  • 非常感谢!我不想保存到服务器。我已经有一个使用 TCPDF 生成 pdf 的页面......但在我的脚本结束时,我使用的是“I”而不是“D”。对“D”的简单更改奏效了!
  • @Webgrity 这会引发错误TCPDF ERROR: Unable to create output file: ../invoice_job_6.pdf
  • @RNKushwaha 尝试使用文件的绝对路径。
【解决方案2】:

改变

<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

中的标题

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 这种情况下,用户下载PDF文件后如何处理?
    • 只需提交表单并使用Basant提到的方法。在这种情况下,文件不是在文件系统上创建的,因此无需担心处置。
    猜你喜欢
    • 2012-04-06
    • 2011-12-17
    • 2011-06-10
    • 2012-08-31
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多