繁体   English   中英

如何使用url将base64中的PDF保存在数据库中?

[英]How to save PDF from base64 in database with url?

我正在生成 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);
}

尝试这个

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

要将 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM