【问题标题】:Open PDF using JavaScript AJAX calling PHP backend使用 JavaScript AJAX 调用 PHP 后端打开 PDF
【发布时间】:2017-05-13 18:05:46
【问题描述】:

我有一个方法,通过 AJAX 调用后端,从 MySQL 数据库中获取一个 blob 文件,由 PHP 检索。

问题是PHP变量包含一个字符串,但是AJAX调用出来是空的,PDF函数不起作用。

这里是被调用的 AJAX 代码。

self.showPDF = function() {
    $.ajax({
            type: 'GET',
            url: BASEURL + 'index.php/myprofile/openUserPDF/' + auth,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
        })
        .done(function(data) {
            console.log(data);
            window.open("data:application/pdf," + escape(data));
        })
        .fail(function(jqXHR, textStatus, errorThrown) {
            alert("Could not open pdf" + errorThrown);
        })
        .always(function(data) {});
}

这是后端的PHP函数,我使用的是CodeIgniter框架。

public function openUserPDF($token) {
    if ($this->input->is_ajax_request()) {
        $userid = $this->myajax->getUserByAuth($token);
        if ($userid) {
            /* If we have an impersonated user in the session, let's use him/her. */
            if (isset($_SESSION['userImpersonated'])) {
                if ($_SESSION['userImpersonated'] > 0) {
                    $userid = $_SESSION['userImpersonated'];
                }
            }
            /* now we get the pdf of the user */
            $this->load->model('user_profile');
            $images = $this->user_profile->getUserImageForPDF($userid);
            $pdfString = $images[0]->image;
            $this->output->set_content_type('application/json');
            return $this->output->set_output(json_encode($pdfString));
        } else {
            return $this->output->set_status_header('401', 'Could not identify the user!');
        }
    } else {
        return $this->output->set_status_header('400', 'Request not understood as an Ajax request!');
    }
}

我确实将 blob 作为字符串检索,但基本上就是这样,它不会返回到 AJAX 调用。

【问题讨论】:

  • 可能性:1)您似乎将 pd f'string' 作为 json 返回。我不确定这是否会起作用-也许作为简单的文本会更好; 2)utf8编码会搞砸一些PDF数据(也许); 3)创建新窗口的方法看起来很狡猾 - afaik window.open() 命令需要一个 URL。
  • @VanquiishedWombat 1) 那么我应该将字符串转换为简单文本吗? 3)我查找了很多打开的 pdf 命令,但由于没有数据返回,所以无法尝试其中任何一个,所以它在那里但没有被使用。
  • 对不起,不是专家,但我可以看到那些可疑的问题。我会在谷歌上搜索“从数据库 ajax 加载显示 pdf”,看看你找到了什么。希望有一些您可以适应的工作代码或教程。如果需要,请务必完善您的问题,如果您根据所学找到答案,请务必发布答案。祝你好运。
  • 请访问此链接在这里您可以找到答案。链接:stackoverflow.com/questions/1106377/….
  • @Bhavin 我没有下载文件,我只想在浏览器中打开它。

标签: javascript php jquery ajax pdf


【解决方案1】:

当您可以简单地使用 file_get_contentsreadfile 直接输出时,将 PDF 的潜在巨大内容加载到内存中是一个非常非常非常非常非常非常的坏主意.

Javascript

self.showPDF = function() {
    window.open(BASE_URL + '/index.php/myprofile/openUserPDF/' + auth);
}

PHP 函数

//... validation and querying ...

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

readfile($pdfString);

(来自How to render pdf file by using php 的标头;有关使用 PHP 渲染 PDF 文件的更多信息,请参阅 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2011-01-01
    相关资源
    最近更新 更多