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条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助