我试图通过jQuery和AJAX从Laravel显示我的PDF,但是以某种方式我无法对其进行可视化,我只能使用二进制文件,但无法显示pdf。
我已经尝试了网络上的所有内容和Stack Overflow(例如对base64进行编码)以及所有给定的解决方案。
PHP / Laravel代码
public function show_pdf(){
//$document = Document::findOrFail("simecarta_inviGOBD930903HQTNRV07.pdf");
/*$project = request('project');
$id = request('id');
$kind = request('kind');*/
//$filePath = $id->file_path;
$name ="simepostulaGOBD930903HQTNRV07.pdf";
$fileName = "sime/simepostulaGOBD930903HQTNRV07.pdf";
//$fileName = $project."/".$project.$kind.$id.".pdf";
// file not found
if( ! Storage::exists($fileName) ) {
abort(404);
}
$pdfContent = Storage::get($fileName);
//$pdfContent = base64_encode($pdfContent);
// for pdf, it will be 'application/pdf'
$type = Storage::mimeType($fileName);
//$fileName = Storage::name($fileName);
return Response::make($pdfContent, 200, [
'Content-Type' => $type,
'Content-Disposition' => 'inline; filename="'.$name.'"'
]);
}//show pdf end
和我的jquery代码
$('#modal_pdf').modal({
closable : false,
onShow : function() {
$.post({
url: 'show_pdf',
data: {_token: $("input[name=_token]").val(),project: 1, id: 2, kind: 3},
dataType: 'json',
success: function (data) {
//$('#id_pdf').attr('src','data:application/pdf;base64,'+data);
alert(1);
console.log(data);
},
complete: function (data){
console.log(data);
//pdfText = $.trim(data);
//pdfText = atob(data);
//var myResponse = eval(data);
/*var blob = new Blob([data], { type: 'application/pdf' });
// create an object URL from the Blob
var URL = window.URL || window.webkitURL;
var d = URL.createObjectURL(blob);*/
var htmlText = '<embed width=100% height=100%'+ ' type="application/pdf"'+ ' src="data:application/pdf,'+ escape(data)+ '"></embed>';
$("#prueba").html(htmlText);
},
});
},
}).modal('show').modal('refresh');
似乎什么都没有解决。