【问题标题】:How to save PDF from base64 in database with url?如何使用url将base64中的PDF保存在数据库中?
【发布时间】:2020-07-02 14:49:24
【问题描述】:

我正在生成 html 表格的 PDF。我想将pdf保存在数据库中。此脚本正在下载 pdf。我想将 pdf 文件发送到控制器端,然后保存在数据库中。我现在在控制器中获取 base64 字符串,如何使用 url 保存和检索?

 var doc = new jsPDF({
       unit: 'px',
       format: 'a4'
 });

    doc.fromHTML($('#revision_table').get(0), 2, 2);
    doc.save('scdedule_revision.pdf');
    var pdf = doc.output();

    axios.post(this.$path + 'api/savePdf', null,
           {
              params: {'pdf_file':  doc.output('datauri')}          
           }
    ).then(({data}) => (
           console.log(data)
         ))
    .catch(error => console.log(error));

控制器:

    public function savePdf(Request $request)
{

    $destinationPath = 'users/pdf';
    $fileuploadedpath = '';
    $pdf = $request->get('pdf_file');
    if ($pdf != '') {
        $extension = $pdf->getClientOriginalExtension();
        $fileName = rand(11111, 99999) . '.' . $extension;
        $success[0] = $pdf->move($destinationPath, $fileName);
        $fileuploadedpath = url($destinationPath . "/" . $fileName);
    }

    dd($fileuploadedpath);
}

【问题讨论】:

    标签: javascript php laravel vue.js


    【解决方案1】:

    要将base64存储到数据库中,您只需将base64字符串存储到数据库列表单控制器

      $b64Doc = base64_encode(file_get_contents($this->pdfdoc));
    

    现在要解码文件,请使用文件来获取 base64 数据表单表,然后使用下面的代码再次获取 PDF

    // a route is created.
    $route    = "pdf/".$name;
    
    // decode base64
    $pdf_b64 = base64_decode($base_64);
    
    // you record the file in existing folder
    if(file_put_contents($route, $pdf_b64)){
    
    //just to force download by the browser
    header("Content-type: application/pdf");
    
    //print base64 decoded
     echo $pdf_b64;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      $data = file_get_contents('string path file');
      $content = base64_decode($data);
      

      【讨论】:

      • 试过下一步做什么?如何保存在 public_storage 中?
      • rename('file_name_current', '/storage/public/file_name');
      • 我们为什么要使用这个?
      猜你喜欢
      • 2021-04-19
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 2017-08-21
      • 2016-01-08
      • 1970-01-01
      相关资源
      最近更新 更多