简体   繁体   English

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

[英]Try to convert xlsx file to html and then to pdf file but got errors NodeJS

I try to convert xlsx file to html table and then to convert the html to pdf.我尝试将 xlsx 文件转换为 html 表,然后将 html 转换为 pdf。 I do this because it is the only way for convert xlsx to pdf.我这样做是因为这是将 xlsx 转换为 pdf 的唯一方法。

I use sheetJS and wkhtmltopdf package.我使用 sheetJS 和 wkhtmltopdf 包。

This is my code:这是我的代码:

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'
    });
});

Error:错误:

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...

A colleague of mine is having the same error using wkhtmltopdf however my system is running great.我的一位同事在使用wkhtmltopdf时遇到了同样的错误,但我的系统运行良好。 I am on Linux with node version 13.9.0.我在 Linux 上使用节点版本 13.9.0。

I used npm search wkhtmltopdf to see the version of my node module node-wkhtmltopdf 2.0.0 wkhtmltopdf 0.3.4我使用npm search wkhtmltopdf查看我的节点模块 node-wkhtmltopdf 2.0.0 wkhtmltopdf 0.3.4 的版本

The affected system is os: mac os mojavi version: 10.14.6 node version: 13.8.0受影响的系统是os: mac os mojavi version: 10.14.6 node version: 13.8.0

What OS, package and node versions are you using?您使用的是什么操作系统、软件包和节点版本?


update 2020-05-06 Our issues seemed in part due to AWS Lambda forcing a new version of node.更新 2020-05-06 我们的问题似乎部分是由于 AWS Lambda 强制使用新版本的节点。 I now have it working in AWS with the following code:我现在使用以下代码在 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); 
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM