Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing filename of a ReadStream in NodeJS

I'm using thecodingmachine/gotenberg for converting office documents to PDF files (gotenberg is using unoconv): Documentation

I have the following code written in javascript (using NodeJS library request) to send a request with a local file to gotenberg:

function openFile(file, fullPath) {
return new Promise((resolve, reject) => {
    const filePath = pathModule.join(fullPath, file);

    var formData = {
        files: fs.createReadStream(filePath),
      };

    request.post({url:"http://docker:3000/convert/office", formData: formData}, function(err, httpResponse, body) {
        if (err) {
           reject('Upload failed!');
        }
        else {
            resolve(body);
        }
    });
});}

When i'm sending to gotenberg a file with an english name, it works.

But when i try to send a filename with a special characters (written in hebrew: בדיקה.docx), gotenberg fails and returns an error:

unoconv: non-zero exit code: exit status 1

This is probably happening because unoconv doesn't support files with an hebrew filename.

Is there any way to change the file name in the file's ReadStream to something like temp.docx instead of בדיקה.docx on the fly, without renaming the file on my server?

Thanks

like image 574
user3688064 Avatar asked Oct 23 '25 17:10

user3688064


1 Answers

You need to change the formData object to the following:

let formData = {
        files: {
            value: fs.createReadStream(filePath),
            options: {
                filename: 'test.docx'
            }
        }
      };

Solved this issue for me :)

like image 64
user3688064 Avatar answered Oct 25 '25 07:10

user3688064