如何从前端的角度将PDF文件发送到laravel

如何从前端的角度将PDF文件发送到laravel

本文介绍了如何从前端的角度将PDF文件发送到laravel后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jsPDF插件将以角度创建的PDF文件发送到我的laravel后端.我尝试获取base64 datauristring.并将其发送到后端.然后,我使用base64解码并将其写入存储.但是我无法获得确切的PDF文件,但有一个错误消息指出该文件已损坏.

I'm trying to send a PDF file created in angular, By the jsPDF plugin to my laravel backend.I tried getting the base64 datauristring. And sent it to the backend. Then I used the base64 decode and wrote it to storage. But I was unable to get the exact PDF file there was an error saying the file is broken .

推荐答案

这取决于您将页面转换为pdf的方式.如果使用addHtml(),则数据字符串以类似data:application/pdf;base64的开头.但是,如果您使用更新版本 jsPDF html(),那么您的datauristring会略有不同,并且看起来像这样data:application/pdf;filename=generated.pdf;base64,.请注意,新字符串filename=generated.pdf;已添加到其中.因此,请确保您正确解码了datauristring.

It depends on how you convert your page to pdf. If you use addHtml(), your datauristring starts with something like this data:application/pdf;base64. But if you use newer version jsPDF and html(), then your datauristring is slightly different and will look like this data:application/pdf;filename=generated.pdf;base64,. Note that a new string filename=generated.pdf; is added to it. So make sure you decode the datauristring correctly.

我使用.NET解码我的datauristring,由于您使用的是php,因此不确定是否对您有帮助.

I use .NET to decode my datauristring, not sure if it will be helpful to you since you are using php.

var match = Regex.Match(strJson, @"data:application/pdf;filename=generated.pdf;base64,(?<data>.+)");
var base64Data = match.Groups["data"].Value;
// or simply strJson.Replace("data:application/pdf;filename=generated.pdf;base64,", "");
var binData = Convert.FromBase64String(base64Data);

这篇关于如何从前端的角度将PDF文件发送到laravel后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:10