libreoffice-convert icon indicating copy to clipboard operation
libreoffice-convert copied to clipboard

Error to convert Html to Docx

Open ViniCleFer opened this issue 10 months ago • 11 comments

I'm executing this code:

const convertAsync = util.promisify(libre.convert);

export async function convertHTMLToWord(file: string) {
  try {
    const extension = '.docx';

    const inputPath = `${__dirname}/example.html`;
    const outputPath = `${__dirname}/example${extension}`;

    await fs.writeFile(inputPath, file);

    const htmlBuffer = await fs.readFile(inputPath);

    const docxBuffer = await convertAsync(htmlBuffer, extension, undefined);

    await fs.writeFile(outputPath, docxBuffer);

    const base64StringWord = await fs.readFile(outputPath, 'base64');

    const mimetype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';

    const dataBase64 = `data:${mimetype};base64,${base64StringWord}`;

    return dataBase64;
  } catch (e) {
    console.error('Error convertHTMLToWord', JSON.stringify(e, null, 2));
  }
}

And I received this:

Error convertHTMLToWord {
  "errno": -2,
  "code": "ENOENT",
  "syscall": "open",
  "path": "/var/folders/q0/kspr59sd4zs2l0qw7ss2qzdh0000gn/T/libreofficeConvert_-63259-vGZ5lZsbRSqP/source..docx"
}

Ca you help me, please?

ViniCleFer avatar Apr 01 '24 20:04 ViniCleFer

maybe try to use the extension without a dot?

elwerene avatar Apr 02 '24 09:04 elwerene

On code of exeplain how to use there is a dot, but I followed your suggestion and remove the dot now and I received the same error.

Error convertHTMLToWord {
  "errno": -2,
  "code": "ENOENT",
  "syscall": "open",
  "path": "/var/folders/q0/kspr59sd4zs2l0qw7ss2qzdh0000gn/T/libreofficeConvert_-72754-J4uShWs5Ba9O/source.docx"
}

ViniCleFer avatar Apr 02 '24 13:04 ViniCleFer

hmm strange, does htmlbuffer contain any bytes?

elwerene avatar Apr 02 '24 13:04 elwerene

Yes

htmlBuffer <Buffer 3c 70 20 73 74 79 6c 65 3d 22 74 65 78 74 2d 61 6c 69 67 6e 3a 20 6a 75 73 74 69 66 79 3b 20 6c 69 6e 65 2d 68 65 69 67 68 74 3a 20 31 2e 35 3b 22 3e ... 65 more bytes>

Error convertHTMLToWord {
  "errno": -2,
  "code": "ENOENT",
  "syscall": "open",
  "path": "/var/folders/q0/kspr59sd4zs2l0qw7ss2qzdh0000gn/T/libreofficeConvert_-89990-URymgrEgBS1s/source.docx"
}

ViniCleFer avatar Apr 02 '24 15:04 ViniCleFer

Do you have libreoffice installed? At which path?

Am 2. April 2024 17:43:22 MESZ schrieb Vinicius Fernandes @.***>:

Yes

htmlBuffer <Buffer 3c 70 20 73 74 79 6c 65 3d 22 74 65 78 74 2d 61 6c 69 67 6e 3a 20 6a 75 73 74 69 66 79 3b 20 6c 69 6e 65 2d 68 65 69 67 68 74 3a 20 31 2e 35 3b 22 3e ... 65 more bytes>

Error convertHTMLToWord {
 "errno": -2,
 "code": "ENOENT",
 "syscall": "open",
 "path": "/var/folders/q0/kspr59sd4zs2l0qw7ss2qzdh0000gn/T/libreofficeConvert_-89990-URymgrEgBS1s/source.docx"
}

-- Reply to this email directly or view it on GitHub: https://github.com/elwerene/libreoffice-convert/issues/117#issuecomment-2032414627 You are receiving this because you commented.

Message ID: @.***>

elwerene avatar Apr 02 '24 16:04 elwerene

Yes, the path is /opt/homebrew/bin/soffice

ViniCleFer avatar Apr 02 '24 19:04 ViniCleFer

Then you need to specify the binary path in the config.

Am 2. April 2024 21:58:49 MESZ schrieb Vinicius Fernandes @.***>:

Yes, the path is /opt/homebrew/bin/soffice

-- Reply to this email directly or view it on GitHub: https://github.com/elwerene/libreoffice-convert/issues/117#issuecomment-2032994048 You are receiving this because you commented.

Message ID: @.***>

elwerene avatar Apr 02 '24 20:04 elwerene

How I do this?

ViniCleFer avatar Apr 02 '24 20:04 ViniCleFer

Change your convert lines to:

const convertAsync = util.promisify(libre.convertWithOptions);

and

const docxBuffer = await convertAsync(htmlBuffer, extension, undefined, {sofficeBinaryPaths: ["/opt/homebrew/bin/soffice"]});

elwerene avatar Apr 02 '24 23:04 elwerene

Doesn't worked

ViniCleFer avatar Apr 03 '24 16:04 ViniCleFer

@ViniCleFer can you set a breakpoint in the convert task? it seems it can not write into your temporary directory. Maybe you can check at the open call if the folder exists. You could set a breakpoint in line 16 of index.js.

elwerene avatar Apr 08 '24 08:04 elwerene

So did you resolve the issue?

elwerene avatar Jun 06 '24 06:06 elwerene