I have downloaded a file through some options, using filesaver or [a]download, but only with the xlsx extension. When I try to download it as a .pdf, the file is downloaded, but it does get corrupted, and I am not able to view it, while the excel file is okay. The excel workbook is created using excel Js.
The piece of code that represents the problem is below.
private exportExcel(workbook: any, filename: string) {
workbook.xlsx.writeBuffer().then((data) => {
console.log(data);
let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const anchor = document.createElement('a');
const url = URL.createObjectURL(blob);
anchor.href = url;
anchor.download = filename;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
URL.revokeObjectURL(url);
const urlpdf = URL.createObjectURL(new Blob([data], {type: "application/pdf"}));
fetch(urlpdf).then(res => res.blob()).then(blob => {
const data = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = data;
a.download = 'file';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(data);
})
}).catch(err => console.log(err))
}
I have tryed it using fetch, not using blob (then I get an error) but none of the options exports a valid .pdf. Thanks in advance.
I am using angular framework