elwerene / libreoffice-convert

MIT License
241 stars 94 forks source link

The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined #50

Closed SCHrodrigo closed 3 years ago

SCHrodrigo commented 3 years ago

As in the title, there is something going on that the "done" variable is coming up short.

I'm on Linux, using node express to host an API to convert a docx that is generated with docxtemplater based on the answers of a form.

This is the code that is converting:

fs.writeFileSync(path.resolve(__dirname,/home/ec2-user/oficial_formulario/documentos/Prospeccao/${nome_docx}.docx`), buf);

const libre = require('libreoffice-convert');

const extend = '.pdf'
const enterPath = path.join(__dirname, `/documentos/Prospeccao/${nome_docx}.docx`);
const outputPath = path.join(__dirname, `/documentos/Prospeccao/${nome_docx}${extend}`);

// Read file
console.log(fs.readdirSync(`/home/ec2-user/oficial_formulario/documentos/Prospeccao/`))
const file = fs.readFileSync(enterPath);
// Convert it to pdf format with undefined filter (see Libreoffice doc about filter)
libre.convert(file, extend, undefined, (err, done) => {
    if (err) {
        console.log(`Error converting file: ${err}`);
    }

    // Here in done you have pdf file which you can save or transfer in another stream
    fs.writeFileSync(outputPath, done);
    res.setHeader('Content-Disposition', `attachment; filename=${nome_docx}.docx`);
    res.sendFile(`/home/ec2-user/oficial_formulario/documentos/Prospeccao/${nome_docx}.docx`);
});`

And this is the error coming up:

Error converting file: Error: Could not find soffice binary internal/fs/utils.js:779 throw new ERR_INVALID_ARG_TYPE( ^

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or >DataView. Received undefined at Object.writeFileSync (fs.js:1460:5) at /home/ec2-user/oficial_formulario/index.js:495:12 at /home/ec2-user/node_modules/libreoffice-convert/index.js:65:20 at /home/ec2-user/node_modules/async/dist/async.js:473:16 at /home/ec2-user/node_modules/async/dist/async.js:1622:17 at /home/ec2-user/node_modules/async/dist/async.js:969:16 at /home/ec2-user/node_modules/libreoffice-convert/index.js:37:32 at /home/ec2-user/node_modules/async/dist/async.js:3377:9 at /home/ec2-user/node_modules/async/dist/async.js:473:16 at iteratorCallback (/home/ec2-user/node_modules/async/dist/async.js:1064:13) at /home/ec2-user/node_modules/async/dist/async.js:969:16 at /home/ec2-user/node_modules/async/dist/async.js:3369:13 at /home/ec2-user/node_modules/libreoffice-convert/index.js:34:68 at FSReqCallback.oncomplete (fs.js:171:23) { code: 'ERR_INVALID_ARG_TYPE' } [nodemon] app crashed - waiting for file changes before

The first console.log on the code shows the file being converted, so I don't know what's going on.

elwerene commented 3 years ago

@SCHrodrigo Is libreoffice installed on your linux server? What's the output of which soffice?

SCHrodrigo commented 3 years ago

@elwerene I used "npm i libreoffice-convert --save" to install it in the directory with index.js, which runs the server.

The output to which soffice were:

$ sudo which soffice which: no soffice in (/sbin:/bin:/usr/sbin:/usr/bin) $ which soffice /usr/bin/which: no soffice in (/home/ec2-user/.local/opt/node/bin:/home/ec2-user/.local/bin:/home/ec2-user/.nvm/versions/node/v14.9.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin)

Both command were executed inside the dir with index.js.

elwerene commented 3 years ago

@SCHrodrigo Please install libreoffice as statet in the Readme. This library only works with a preinstalled instance of libreoffice. If that's not an option on your server, then this lib won't help you convert files. Sorry

SCHrodrigo commented 3 years ago

Hey, so I just got libreoffice installed, the message from which soffice is

/opt/libreoffice7.0/program/soffice

I confirmed there is a file soffice and soffice.bin, but the Could not find soffice binary error persists.

Sorry for dragging this.

elwerene commented 3 years ago

@SCHrodrigo no worries, soffice needs to be in "/usr/bin/soffice". you could link it there:

ln -s /opt/libreoffice7.0/program/soffice /usr/bin/soffice

There's an issue to set another installation folder, but nobody actually provided a pr for it yet: https://github.com/elwerene/libreoffice-convert/issues/49

SCHrodrigo commented 3 years ago

Hot damn, it worked! Thank you so much for helping!

manusiakemos commented 2 months ago

i have the same error. image

i installed libre and nodejs on same container service image

here is my express js code on app.js file

const libre = require('libreoffice-convert');
const {fi} = require("@faker-js/faker");
libre.convertAsync = require('util').promisify(libre.convert);
app.post("/upload-dokumen",
    async (req, res) => {
        const ext = '.pdf'
        const inputPath = path.resolve(__dirname, './resources/example.txt');
        const outputPath = path.resolve(__dirname, `/resources/example${ext}`);
        const file =fs.readFileSync(inputPath);
        libre.convert(file,ext, undefined, function (err, done){
            fs.writeFileSync(outputPath, done);
            res.json({
                message: 'express js boilerplate',
                text: 'upload dokumen',
                data: {
                    path: outputPath
                }
            });
        });
    }
);
manusiakemos commented 2 months ago

fixed after change from path.resolve to join

const inputPath = path.join(__dirname, './resources/example.docx');
const outputPath = path.join(__dirname, `/resources/example${ext}`);