doubi4435 2016-03-24 13:57
浏览 67
已采纳

Laravel 5.2 - 如何将PDF作为JSON响应的一部分返回

So I have developed an API and one of the endpoints is a report generator. Normally they send in the name of the report and some other variables and it pops out a PDF using Laravel-Snappy.

I am also using JWT-Auth for authentication. Since I need to be HIPAA compliant I will be refreshing the token on EVERY call. For most endpoints this is easy as I just add it to the meta of my json response.

However, for the reports they only return a PDF with headers to force download etc. Is there a way for me to send the PDF data as part of a JSON response that applications hitting this API would be able to use? Do I maybe need to encrypt it in some way that it can be sent with a json response? Any other ideas on any line of thought would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongzi0850 2016-03-24 15:21
    关注

    Never had to do this personally, but the way I've seen it in APIs is to read the file contents, base64 encode and serve that up in the json response.

    Quick bit of code

    $pdf = base64_encode(file_get_contents('path/to/my.pdf'));
    
    return response()->json([
        'pdf' => $pdf,
    ]);
    

    The user can then consume this as such.

    $pdf = base64_decode($json->pdf);
    
    file_put_contents('path/to/save/my.pdf', $pdf);
    

    You should then be able to open/read the saved file as normal.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站