dow5001 2014-09-01 21:04
浏览 131
已采纳

使用PHP DOMPDF和AJAX下载PDF文件

I have this PHP File called print.php:

<?php
require_once("modules/pdf/dompdf_config.inc.php");
$html = $_POST["toRender"];
$number = $_POST["number"];
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($number.".pdf");

As you can see, the HTML and a Number are received via POST.

The JavaScript file looks like this:

 $("#btnViewPDF").click(function() {

        var theHtml = $("#printable").html();
        var theNumber = $("#invoiceNumber").val();

        $.ajax({
            type : "post",
            url  : "views/print.php",
            data : {toRender : theHtml, number : theNumber},
            beforeSend: function() {
                $(".ajax-loader-bg").fadeIn();
            }
        }).always(function() {
            $(".ajax-loader-bg").fadeOut();
        });
        return false;
    });

Basically it takes all the content inside a DIV called 'printable' but what I want next is the PDF that has been generated in print.php to be displayed, I haven't been able to figure out how can I make this.

I've made it work when I generate test HTML inside print.php and then I type in the url mysite.com/print.php ... it renders the PDF and allows me to download it or see it in another browser tab.

How can I achieve this via AJAX?

  • 写回答

2条回答 默认 最新

  • dongxichan8627 2014-09-01 23:08
    关注

    You can't download something via AJAX, you could simulate the behavior using an hidden iframe.

    Not that you can't download it, but, it will never end up in the filesystem for saving purpose because javascript can't do that for security reasons.

    Anyway people always find solutions, you can try this: https://stackoverflow.com/a/23797348/1131176

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?