How can I send PDF file from my angular fronted to my laravel backend

你。 提交于 2020-01-03 03:23:09

问题


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 .


回答1:


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.

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


来源:https://stackoverflow.com/questions/54489788/how-can-i-send-pdf-file-from-my-angular-fronted-to-my-laravel-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!