Convert Excel to PDF programmaticaly

て烟熏妆下的殇ゞ 提交于 2019-12-11 06:19:29

问题


I am using 'exceljs' module in my Angular project to build an Excel file. Instead of exporting it as Excel file, I would first like to convert it to PDF and then download it as such.

I can successfully export and save this file to my computer:

    workbook.xlsx.writeBuffer().then((data) => {
        console.log(data); // Uint8Array
        console.log(data.buffer); // ArrayBuffer
        console.log(new Blob([data])); // Blob

        // code I use to export it as an Excel File
        const blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'});
        const test = FileSaver.saveAs(blob, 'test.xlsx');
    });

As written above, instead of an Excel file I would like to have it exported as a PDF file.

From the code you can see that 'exceljs' is giving me an Uint8Array stream of data, from which I can get out an ArrayBuffer, or I can convert it to a Blob.

I have tried using this without success:

workbook.xlsx.writeBuffer().then((data) => {
    const blob = new Blob([data], { type: 'application/pdf' });
    // or with 'application/octet-stream' type
    // const blob = new Blob([data], { type: 'application/octet-stream' });
    FileSaver.saveAs(blob, 'test.pdf');
});`

The PDF file gets exported, but it cannot be opened. I get an error like "The file is corrupted."

Thank you for all the help in advanced.

来源:https://stackoverflow.com/questions/54608270/convert-excel-to-pdf-programmaticaly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!