gpt4 book ai didi

node.js - 尝试将 xlsx 文件转换为 html,然后转换为 pdf 文件,但出现错误 NodeJS

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:39 28 4
gpt4 key购买 nike

我尝试将 xlsx 文件转换为 html 表,然后将 html 转换为 pdf。我这样做是因为这是将 xlsx 转换为 pdf 的唯一方法。

我使用sheetJS和wkhtmltopdf包。

这是我的代码:

const XLSX = require('xlsx');
const wkhtmltopdf = require('wkhtmltopdf');

router.get('/xlsx', async (request, response) => {
var xlsFile = fs.readFileSync('./uploads/temp/excel.xlsx');
var html = XLSX.read(xlsFile, { type:'buffer' });
const finalFile = XLSX.write(html, { type: 'binary', bookType: 'html' });
wkhtmltopdf(finalFile, {
output: './uplods/temp/xlsx.pdf',
'viewport-size': '1280x1024',
'page-width': '400',
'page-height': '600'
});
});

错误:

events.js:298
throw er; // Unhandled 'error' event
^

Error: write EPIPE
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16)
Emitted 'error' event on Socket instance at:
at errorOrDestroy (internal/streams/destroy.js:128:12)
at onwriteError (_stream_writable.js:463:3)
at onwrite (_stream_writable.js:484:7)
at internal/streams/destroy.js:60:7
at Socket._destroy (net.js:677:5)
at Socket.destroy (internal/streams/destroy.js:55:8)
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:93:12) {
errno: -32,
code: 'EPIPE',
syscall: 'write'
}
[nodemon] app crashed - waiting for file changes before starting...

最佳答案

我的一位同事在使用 wkhtmltopdf 时遇到了同样的错误,但我的系统运行良好。我使用的 Linux Node 版本为 13.9.0。

我使用npm search wkhtmltopdf来查看我的 Node 模块的版本 Node -wkhtmltopdf 2.0.0wkhtmltopdf 0.3.4

受影响的系统是操作系统:mac os mojavi 版本:10.14.6 Node 版本:13.8.0

您使用的操作系统、软件包和 Node 版本是什么?

<小时/>

更新2020-05-06我们的问题部分似乎是由于 AWS Lambda 强制使用新版本的 Node 造成的。我现在可以使用以下代码在 AWS 中运行它:

return wkhtmltopdf(html)
.then(buffer => {

return {
statusCode: 200,
headers: {

'Content-type': 'application/pdf',
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": true,
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Expose-Headers': 'DAV, content-length, Allow'
},
body: JSON.stringify({"b64": buffer.toString('base64')}),
}
}).catch(error => {
let msg = JSON.stringify({
message: "s Internal server error ",
error, html
})
return returnError(msg);
});

关于node.js - 尝试将 xlsx 文件转换为 html,然后转换为 pdf 文件,但出现错误 NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com