首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Ajax将PDF资源从PHP下载到Javascript

通过Ajax将PDF资源从PHP下载到Javascript
EN

Stack Overflow用户
提问于 2018-06-07 03:10:38
回答 2查看 1.2K关注 0票数 0

因此,我将解释这个问题:

步骤:

1)客户端(浏览器javascript)向服务器发送Ajax请求,该请求命中名为download的控制器方法。

2)控制器的方法创建一个PDF资源(不保存在文件系统上),并将带有PDF二进制流的响应返回给客户端。

3)客户端接收PDF二进制流并将其下载到客户端计算机上。这有可能吗?

代码:我已经尝试过的东西-

客户端:

<script>
    (function($) {

        var button; // some random DOM button

        button.on('click', function(e) {
            e.preventDefault();

            $.ajax({
                url: "/download/:userId"
                method: "POST",
                dataType: "json"
                success: function(response) {
                    var reader = new FileReader;
                    var file = new Blob([response.pdf_stream], 'application/pdf');

                    // create a generic download link
                    var a = $('<a/>', {
                        href: file,
                        download: response.filename
                    });

                    // trigger click event on that generic link.
                    a.get(0).click(); 
                }
            });
        }

    })(jQuery);


</script>

在服务器端:

class Controller
{
     public function download($userId)
     {
         // fetching the user from the database
         $user = User::find($userId);

         // creating a pdf file using barry pdfdom package
         // this will actually parse an HTML view and give us the PDF blob.
         $pdf = PDF::loadView('pdf.view')->output();

         // using Laravel helper function
         return response()->json([
             'pdf_stream' => utf8_encode($pdf),
             'filename' => 'blahblah.pdf"
         ]);

        // Or if you will in native PHP, just in case you don't use laravel.
        echo json_encode([
             'pdf_stream' => utf8_encode($pdf),
             'filename' => 'blahblah.pdf"
        ]);
     }
}

你知道我哪里做错了吗?如何下载PDF文件而不将其保存到系统(安全和空间问题)。

任何帮助都将不胜感激。

伊甸园

EN

回答 2

Stack Overflow用户

发布于 2018-06-07 03:37:13

如果您想在客户端下载pdf,只需在新窗口中打开此pdf即可。使用GET请求,比如在RESTfull应用程序中(例如,download/user/:id或类似的东西)。

可能有用:Download and open pdf file using Ajax

票数 0
EN

Stack Overflow用户

发布于 2018-06-07 15:27:55

主要问题是控制器返回的响应。试试这个:

public function download($userId)
     {
      // fetching the user from the database
      $user = User::find($userId);

      // creating a pdf file using barry pdfdom package
      // this will actually parse an HTML view and give us the PDF blob.
      $pdf = PDF::loadView('pdf.view')->output();
      return response($pdf, 200,
        [
          'Content-Type'   => 'application/pdf',
          'Content-Length' =>  strlen($pdf),
          'Cache-Control'  => 'private, max-age=0, must-revalidate',
          'Pragma'         => 'public'
        ]
      );

关于调用执行download($userid)方法的路由:

您不必使用Ajax。简单的方法:

<a href="/path/to/download/1" target="_blank">Click view PDF</a>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50727918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档