代码之家  ›  专栏  ›  技术社区  ›  oliverbj

Laravel-从原始内容创建PDF文件-JSON API

  •  0
  • oliverbj  · 技术社区  · 7 年前

    我正在尝试从原始PDF字符串创建一个临时PDF文件。

    这是我的输入,通过API(JSON)发送:

    {
       "name": "pdffilename.pdf",
       "content": "%PDF-1.2 [.......]%%EOF"
    }
    
    • 这个 "content" 字符串是实际的PDF原始数据字符串。

    这是我的控制器,处理API请求:

    /**
     * Function to convert a PDF file to text
     */
    public function PDFtoText(Request $request)
    {
        $name = $request->name;
        $content = $request->content;
    
        //Save PDF file on the server (temp files).
        $pdf = Storage::disk('local')->put('/temp_files/' . $name, $content);
    
        return response()->json([
            'result' => "Success"
        ], 200);
    }
    

    将在中创建一个实际文件 temp_files 文件夹,名称为 pdffilename.pdf .但是我无法打开文件,因为它说文件“已损坏”。

    我做错了什么?

    0 回复  |  直到 7 年前
        1
  •  -1
  •   Cristhian Barros    6 年前

    保存文档或文件内容的最佳方法是使用base64_encode函数,该函数在base64 encode中转换内容。然后,当您需要这些内容时,您需要做的唯一过程是调用base64_decode函数。我试过了,效果很好。任何问题只给我写信。