I am attempting to download a PDF that is generated by my server when a user clicks on a download button. I have gotten this to work, but it does not seem like best practice since the PDF opens in a new window using a server URL:
public exportPdf() {
const link = document.createElement('a');
link.hidden = true;
link.target = '_blank;';
link.download = 'test.pdf';
link.href = `blob:http://localhost:3333/pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
link.remove();
}
So now I am trying to instead use a generated front-end URL, but the PDF window never opens. I am able to go to the console link that is printed out, and see my PDF just fine. Am I missing something else in this second implementation?
public exportPdf() {
this.httpClient.get(`http://localhost:3333/pdf`, { responseType: 'blob' } )
.subscribe(response => {
const link = document.createElement('a');
link.hidden = true;
link.target = '_blank;';
link.download = 'test.pdf';
link.href = window.URL.createObjectURL(response);
console.log({link})
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
link.remove();
})
}