html
<div style="height: 600px">
<ng2-pdfjs-viewer #pdfViewer></ng2-pdfjs-viewer>
</div>
typescript
<!-- your.component.ts -->
@ViewChild('pdfViewer') public pdfViewer;
constructor(private http: HttpClient) {
let url = "api/document/getmypdf";
this.downloadFile(url).subscribe(
(res) => {
this.pdfViewer.pdfSrc = res;
this.pdfViewer.refresh();
}
);
}
private downloadFile(url: string): any {
return this.http.get(url, { responseType: 'blob' })
.pipe(
map((result: any) => {
return result;
})
);
}
Server Side (Example in C#)
[HttpGet]
[Route("GetMyPdf")]
public IActionResult GetMyPdf()
{
var pdfPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/pdfjs/web/gre_research_validity_data.pdf");
byte[] bytes = System.IO.File.ReadAllBytes(pdfPath);
return File(bytes, "application/pdf");
}