如何使用FileSaver.js在Angularjs中将以下内容保存为PDF

tph*_*312 1 api angularjs filesaver.js

图片

我无法使用FileSaver.js将此内容另存为正确的PDF


这是Angular代码

$http(
  { url:root.api_root+'/appointments/generatePDF/'+$route??Params.id,
    method:'GE??T'
}).then(function(r??es){ // 
    $log.log(res.data); 
    var blob = new Blob([res.data], { type: 'application/pdf' });
    window.open(res.data); 
    // saveAs(blob, 'file.pdf');
});
Run Code Online (Sandbox Code Playgroud)

这是TCPDF后端代码

$pdf->Output("php://output", 'F'); 
echo file_get_contents("php://output");
Run Code Online (Sandbox Code Playgroud)

geo*_*awg 5

下载PDF文件等二进制数据时,设置responseType属性非常重要:

$http(
  { url:root.api_root+'/appointments/generatePDF/'+$route??Params.id,
    method:'GE??T',
    //IMPORTANT
    responseType: 'blob'
}).then(function(r??es){ // 
    $log.log(res.data);
    //var blob = new Blob([res.data], { type: 'application/pdf' });
    //window.open(res.data);
    var blob = res.data; 
    saveAs(blob, 'file.pdf');
});
Run Code Online (Sandbox Code Playgroud)

如果responseType省略该属性,则XHR API默认将数据处理为UTF-8文本.将数据解码为UTF-8文本的过程将破坏PDF或图像等二进制文件.

有关更多信息,请参阅MDN Web API参考 - XHR ResponseType