我正在使用 ng2-pdfjs-viewer,我可以传递一个 pdf 文件 http url,但我不能使用插值,也不能将变量传递给 html 标记内的 pdfSrc 选项,以从休息服务响应中呈现 pdf。
如何将 Uint8array 或 base64 对象从 component.ts 传递到 component.html?
我正在使用 ng2-pdfjs-viewer,我可以传递一个 pdf 文件 http url,但我不能使用插值,也不能将变量传递给 html 标记内的 pdfSrc 选项,以从休息服务响应中呈现 pdf。
如何将 Uint8array 或 base64 对象从 component.ts 传递到 component.html?
此处提供了您案例的示例:https ://ng2-pdfjs-viewer.azurewebsites.net/bytearray 。
代码提取
你的.component.html
<div style="height: 600px">
<ng2-pdfjs-viewer #pdfViewer></ng2-pdfjs-viewer>
</div>
你的.component.ts
@ViewChild('pdfViewer') public pdfViewer;
constructor(private http: HttpClient) {
let url = "api/document/getmypdf";
this.downloadFile(url).subscribe(
(res) => {
this.pdfViewer.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
this.pdfViewer.refresh(); // Ask pdf viewer to load/refresh pdf
}
);
}
private downloadFile(url: string): any {
return this.http.get(url, { responseType: 'blob' })
.pipe(
map((result: any) => {
return result;
})
);
}