elwerene / libreoffice-convert

MIT License
241 stars 94 forks source link

Error: ENOENT: no such file or directory, open '/tmp/libreofficeConvert_-32447-PWkldkvtqA0b/source..docx' #81

Closed roverboy773 closed 1 year ago

roverboy773 commented 2 years ago

Error occurs when convert function is called

const path = require("path"); const fs = require("fs").promises;

const libre = require("libreoffice-convert");
libre.convertAsync = require("util").promisify(libre.convert);

(async function fn() {
  try {
    const ext = req.body.convertTo;

    const temp1 = req.body.data.resp.fileName.lastIndexOf(".");

    const inputPath = path.join(__dirname, "../", req.body.data.resp.path);
    const outputPath = path.join(
      __dirname,
      "../",
      `uploads/${req.body.data.resp.fileName.substring(0, temp1)}${ext}`
    );

    // Read file
    const docxBuf = await fs.readFile(inputPath);

    // Convert it to pdf format with undefined filter (see Libreoffice docs about filter)
    libre.convert(docxBuf, ext, undefined, (err, done) => {
      if (err) {
        console.log(docxBuf);
        console.log(`Error converting file: ${err}`);
        res.sendStatus(500);
      } else {
        console.log(done);
      }
    });
  } catch (err) { 
    console.log(err);
  }
})();

output Error:Error converting file: Error: ENOENT: no such file or directory, open '/tmp/libreofficeConvert_-3947-L12WFnW9nmeG/source..docx'

roverboy773 commented 2 years ago

req.body.data:={ file: 'http://localhost:5000/files/a952e17d-aaed-4063-a771-bc726618df7f', resp: { expire_at: '2022-03-31T09:20:35.275Z', _id: '6245740e1b32170e250e910f', fileName: '1648718862144-0.9536543514182461.pdf', path: 'uploads/1648718862144-0.9536543514182461.pdf', size: 233836, uuid: 'a952e17d-aaed-4063-a771-bc726618df7f', file_owner: '61de5f132999590a308ecfdb', createdAt: '2022-03-31T09:27:42.154Z', updatedAt: '2022-03-31T09:27:42.154Z', __v: 0 } }

elwerene commented 2 years ago

did you try running your code on a local file? This issue needs a lot more context!

k163791 commented 2 years ago

I ran into the same issue, i was using a local file, but the thing is i was trying to convert from pdf to docx, when i converted from docx to pdf everything ran smoothly, but i couldn't figure out why it was not working the other way around.

Does the library support conversions from pdf to other formats ?

roverboy773 commented 2 years ago

did you try running your code on a local file? This issue needs a lot more context!

yes, I used a local file, the server is running locally

roverboy773 commented 2 years ago

I ran into the same issue, i was using a local file, but the thing is i was trying to convert from pdf to docx, when i converted from docx to pdf everything ran smoothly, but i couldn't figure out why it was not working the other way around.

Does the library support conversions from pdf to other formats ?

I haven't tried the other way out...I think the purpose of the library is to interconvert right?....rather than convert all to pdfs

k163791 commented 2 years ago

I ran into the same issue, i was using a local file, but the thing is i was trying to convert from pdf to docx, when i converted from docx to pdf everything ran smoothly, but i couldn't figure out why it was not working the other way around. Does the library support conversions from pdf to other formats ?

I haven't tried the other way out...I think the purpose of the library is to interconvert right?....rather than convert all to pdfs

Yes, exactly.. that's what i thought, but when converting to pdf its working fine for me, the issue only happens when i try to convert from pdf to another format

k163791 commented 2 years ago

I have also tried multiple filters thinking that maybe that's the issue but the error is still the same

elwerene commented 2 years ago

Did you try opening a pdf file in libreoffice and convert it into docx? Does this work? It seems you are trying to do something which is not supported by libeeoffice. As this library only calls libeeoffice to convert files, you might want to open an issue in the libreoffice bugzilla.

k163791 commented 2 years ago

You're correct, I looked this up and Libreoffice actually doesn't support such thing, do you know of such a library or open source tool that does this ? convert pdf to word ? If so please do tell me. Thanks and apologies for troubling you.

elwerene commented 1 year ago

No, I don't know a library which is able to do this. You could maybe rasterize the pdf with imagemagick and convert it to a word document then. But I guess that's only a small part of possible use cases.

hilmanski commented 1 year ago

Hi @k163791 what's your take on this? currently wondering is this library support pdf -> to other file? or only otherformat -> pdf?

Or you found a ways with other library to convert from PDF?

Thanks

galaczi commented 7 months ago

I am facing the same issue. Converting from pdf to html works. Converting to docx gives this error. Doesn't seem to be a LibreOffice issue. It swiftly converts the pdf to docx.