elwerene / libreoffice-convert

MIT License
241 stars 94 forks source link

Error in API route: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined #109

Closed victorfg closed 6 months ago

victorfg commented 7 months ago

Hi,

Im having this error in my application in windows OS:

Error in API route: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined at new NodeError (node:internal/errors:399:5) at validateString (node:internal/validators:163:11) at Object.join (node:path:429:7) at soffice (C:\Users\alba\Desktop\guardiesResidents\node_modules\libreoffice-convert\index.js:25:26) at runTask (C:\Users\alba\Desktop\guardiesResidents\node_modules\async\dist\async.js:1155:17)

which soffice /c/Program Files/LibreOffice/program/soffice

soffice --version LibreOffice 7.6.3.2

I test the same aplication in macOS and it works:

which soffice /opt/homebrew/bin/soffice

soffice --version LibreOffice 7.6.2.1

The error appears in this code: (in libre.convert function)

import ExcelJS from "exceljs";
import libre from "libreoffice-convert";

function convertToPDF(buffer) {
  return new Promise((resolve, reject) => {
    libre.convert(buffer, ".pdf", undefined, (err, done) => {
      if (err) {
        console.log("Error converting to PDF:", err);
        reject(err);
      } else {
        console.log("done", done);
        resolve(done);
      }
    });
  });
}

export default async (req, res) => {
  if (req.method === "POST") {
    try {
      const buffer = Buffer.from(req.body.excelFile, "base64");

      const workbook = new ExcelJS.Workbook();
      await workbook.xlsx.load(buffer);

      const pdfBuffer = await convertToPDF(buffer);

      res
        .status(200)
        .json({ pdfBuffer: pdfBuffer.toString("base64"), error: null });
    } catch (error) {
      console.error("Error in API route:", error);
      res.status(500).json({ error: error.message });
    }
  } else {
    res.setHeader("Allow", ["POST"]);
    res.status(405).end(`Method ${req.method} Not Allowed`);
  }
};

Thanks

elwerene commented 6 months ago

It seems like it is not finding the executable. Did you try specifying the path to the executable?

victorfg commented 6 months ago

Hi @elwerene , you mean that /c/Program Files/LibreOffice/program/soffice points to the .exe file? How can I do this? Thanks

elwerene commented 6 months ago

@victorfg see here: https://github.com/elwerene/libreoffice-convert/blob/master/index.d.ts#L16C7-L16C25

victorfg commented 6 months ago

After adapt my code an call libre.convertWithOptions the error is the same. My code now it looks like this:


import libre from "libreoffice-convert";
const fs = require('fs');
const path = require('path')

function convertToPDF(buffer) {
  return new Promise((resolve, reject) => {
    const options = {
      sofficeBinaryPaths: [],
    };

    const sofficePaths = [
      path.join(process.env.PROGRAMFILES, 'LibreOffice\\program\\soffice.exe'),
      path.join(process.env.PROGRAMFILESX86, 'LibreOffice\\program\\soffice.exe')
    ];

    // Add the first route valid
    for (const p of sofficePaths) {
      if (fs.existsSync(p)) {
        options.sofficeBinaryPaths.push(p);
        break;
      }
    }

    if (options.sofficeBinaryPaths.length === 0) {
      console.error('there is no soffice.exe route');
      return;
    }

    libre.convertWithOptions(
      buffer,
      ".pdf",
      undefined,
      options,
      (err, done) => {
        if (err) {
          console.log("Error converting to PDF:", err);
          reject(err);
        } else {
          console.log("done", done);
          resolve(done);
        }
      }
    );
  });
}

and the error:

Error in API route: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:399:5)
    at validateString (node:internal/validators:163:11)
    at Object.join (node:path:429:7)
    at eval (webpack-internal:///(api)/./pages/api/excel-to-pdf.js:26:32)
    at new Promise (<anonymous>)
    at convertToPDF (webpack-internal:///(api)/./pages/api/excel-to-pdf.js:21:10)
    at __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(api)/./pages/api/excel-to-pdf.js:66:31)
    at async Object.apiResolver (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\api-utils\node.js:366:9)
    at async DevServer.runApi (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\next-server.js:481:9)    
    at async Object.fn (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\next-server.js:741:37)
    at async Router.execute (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\router.js:252:36)
    at async DevServer.run (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\base-server.js:365:29)      
    at async DevServer.run (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\dev\next-dev-server.js:709:20)
    at async DevServer.handleRequest (C:\Users\alba\Desktop\GUÀRDIES RESIDENTS\guardiesResidents\node_modules\next\dist\server\base-server.js:303:20) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Any idea how to fix it? Thanks

danperks commented 4 months ago

I'm getting this same error, including after providing the binary path directly to soffice.exe.

image

Any suggestions?