簡體   English   中英

下載生成的 PDF,在客戶端使用 FileSaver,在服務器端使用 Snappy

[英]Download generated PDF use FileSaver on client side and Snappy on server side

我嘗試使用 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 會導致問題,但我不知道如何解決。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM