我尝试使用FileSaver从服务器下载生成的PDF。但是,它不起作用。
在Laravel框架中,我使用Snappy从HTML创建PDF文件。
$pdf = \PDF::loadView('pub.view', $data);
//$pdf->save('myfile.pdf'); //It's saved successfully.
return $pdf->download('printing.pdf');
从客户端,我使用AngularJS的$ resource服务来获取PDF文件。
printPDF: {
method: 'GET',
headers: {
accept: 'application/pdf'
},
responseType: 'arraybuffer',
cache: true,
transformResponse: function (data) {
var pdf;
if (data) {
pdf = new Blob([data], {
type: 'application/pdf'
});
}
return {
response: pdf
};
},
url:'/u/:id/exportTopdf',
params:{id:'@id'}
}
服务器响应后,FileSaver将保存此文件。
myService.printPDF({id: myId},
function successCallback(response){
saveAs(response, 'filename.pdf'); //FileSaver function
}, function errorCallback(response){
});
运行时,问题出现在浏览器控制台中:
`TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided`.
我认为FileSaver会解决问题,但我不知道如何修复它。
答案 0 :(得分:1)
param = {
action: "downloadReport",
};
$http({
method: 'POST',
url: 'http://localhost:8080/ivftcwcm2/services/fundTransferServices/downloadFile',
//url:'http://localhost:8080/FundTransferUI/resources/locales/Disclosure.pdf',
data: param,
responseType: 'arraybuffer'
}).then(function(response) {
var file = new Blob([response.data], {
type: 'application/pdf'
});
var fileURL = URL.createObjectURL(file);
var a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = 'pdfFile.pdf';
document.body.appendChild(a);
a.click();
}, function(response) {
alert('call failed');
});