将使用excel.js生成的excel转换为pdf

时间:2019-10-21 13:56:16

标签: javascript excel angular pdf exceljs

我想将使用exceljs模块(角度)生成的excel转换为PDF。 这是我生成excel的代码。

workbook.xlsx.writeBuffer().then((data) => {
    // 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');
});

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。您可以使用libreConvert(只需安装libreoffice)。这是确切的代码:

      // require fs module to read excel file and store pdf
      const fs = require('fs');
      // require promisify from bluebird to be able to await conversion to pdf
      const { promisify } = require('bluebird');
      // require libre and promisify the function to convert
      const libre = require('libreoffice-convert');
      const libreConvert = promisify(libre.convert);

      // read created excel file (this should be in an async function)
      let data = await fs.promises.readFile(path_to_excel_file);
      // convert to pdf 
      let pdfFile = await libreConvert(data, '.pdf', undefined);
      // store new created pdf
      await fs.promises.writeFile(`${dirName}/${docName}.pdf`, pdfFile);