将使用 ExcelJS 生成的 Excel 转换为 PDF



我想将我生成的带有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');
});

我遇到了同样的问题。您可以使用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);

最新更新