问题
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