Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SCHrodrigo opened this issue Jan 13, 2021 · 8 comments

Comments

@SCHrodrigo
Copy link

SCHrodrigo commented Jan 13, 2021

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
Copy link
Owner

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

@SCHrodrigo
Copy link
Author

@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
Copy link
Owner

@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
Copy link
Author

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
Copy link
Owner

@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: #49

@SCHrodrigo
Copy link
Author

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

@manusiakemos
Copy link

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
Copy link

fixed after change from path.resolve to join

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants