![](/img/trans.png)
[英]How can I determine the extent (in bytes) of page 1 in a linearized PDF file?
[英]How can I open a PDF from array of bytes in Angular 9?
我有一个 WebApi 方法,可将字节数组返回到角度前端。 我已经尝试了所有方法,从将其作为 Blob 读取,将其作为数组缓冲区读取,将其从字节数组转换为 Blob,但没有任何效果。 每次我收到无法打开 pdf 错误。
组件中的示例代码:
this.service.getPDF().subscribe((response)=>{
let file = new Blob([response], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
service.ts 中的示例代码:
getPDF(){
const url = `${this.serviceUrl}/pdf`;
const httpOptions = {
'responseType' : 'arraybuffer' as 'json'
//'responseType' : 'blob' as 'json' //This also worked
};
return this.http.get<any>(url, httpOptions);
}
问题可能存在于 Angular 的 HttpClient 中,它由于某种原因不能正确地将您的响应读取为数组缓冲区。 尝试将其转换为 Uint8Array 之前,它可以为您提供适当的结构,准备进一步处理:
试试看:
const blob = new Blob([new Uint8Array(byteArrays)], { type: "application/pdf" }); const exportUrl = URL.createObjectURL(blob); window.open(exportUrl); URL.revokeObjectURL(exportUrl);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.